)]}'
{
  "commit": "9e10fcf71c1101fb6422d0f52de5e615ed8df71d",
  "tree": "85b5ac5c4bd81f0a8a0122c4ba45c0f2a476bc4d",
  "parents": [
    "9e8b20dbd8e3c9c29f4cc3923d4566450fe1f7e8"
  ],
  "author": {
    "name": "Nelson Chu",
    "email": "nelson@rivosinc.com",
    "time": "Thu Jul 03 11:16:24 2025 +0800"
  },
  "committer": {
    "name": "Nelson Chu",
    "email": "nelson@rivosinc.com",
    "time": "Tue Jul 22 14:15:55 2025 +0800"
  },
  "message": "RISC-V: Fix the assert fail when linking discarded sections under -pie for got\n\nConsidering the following case,\n% cat tmp.s\n.option pic\n.text\n.global _start\n_start:\nnop\n.section .discard.s, \"ax\"\nla x1, _start\n% cat tmp.ld\nOUTPUT_ARCH(riscv)\nENTRY(_start)\nSECTIONS\n{\n  /DISCARD/ : { *(.discard.*) }\n  . \u003d 0x10000;\n  .text : { *(.text) }\n  . \u003d 0x20000;\n  .got  : { *(.got) *(.got.plt)}\n  . \u003d 0x30000;\n  .data : { *(.data) *(.data.*) }\n}\n% riscv64-unknown-linux-gnu-as tmp.s -o tmp.o\n% riscv64-unknown-linux-gnu-ld -pie -Ttmp.ld tmp.o\nriscv64-unknown-linux-gnu-ld: BFD (GNU Binutils) 2.44.50.20250624 assertion fail binutils-gdb/bfd/elfnn-riscv.c:3638\n\nThis happens when pie and the input sections, which refers to the global\nsymbol by got, are all discarded.  Since referenced sections are all discarded,\nwe won\u0027t go into relocate_section for those sections, the got entry also won\u0027t\nbe initialized.  Therefore, we will get assert fail when adding the RELATIVE\nreloc in the finish_dynamic_symbol.\n\nAfter seeing other target codes, there are two root causes as follows,\n\n1. risc-v may call bfd_elf_link_record_dynamic_symbol in the allocate_dynrelocs\n   for not only undefweak symbols.\n\n2. risc-v is missing the code to add RELATIVE to R_RISCV_GOT entries in the\n   relocate_section if a symbol is not dynamic and is not undefined weak under\n   pic and pie.\n\nIf we call bfd_elf_link_record_dynamic_symbol, then the global symbol will be\nforced to dynamic, so the h-\u003edynindx will forced to be a number rather than -1,\neven it should be -1.  Once h-\u003edynindx !\u003d -1 and pic/pie, it will go into\nfinish_dynamic_symbol and insert RELATIVE/64 relocs for the got entry;  For the\nabove case there are two issues,\n\n1. The global symbol _start is forced to be dynamic in the allocate_dynrelocs.\n   when pie and all the referenced section are discarded, it won\u0027t go into\n   relocate_section to initialize the got entry, so it will cause assert fail\n   when adding RELATIVE reloc in the finish_dynamic_symbol.  The assert fail\n   represents another problem - if we don\u0027t initialize the got entry in the\n   relocate_section under pie, which means we don\u0027t need to go into the\n   finish_dynamic_symbol and don\u0027t need a RELATIVE reloc for the got entry,\n   it should be NONE reloc.\n\n2. Without linking any discarded section, it originally forces every RELATIVE\n   relocs added for every got by the finish_dynamic_symbol.  Even The final\n   result looks correct under pie (genearte a RELATIVE reloc for got entry),\n   not sure if it may cause other problems for some special cases, excpet the\n   above one.\n\nTherefore, this patch try to fix the above assert fail, and also clarify the\nbehavior of the allocate_dynrelocs which should only call bfd_elf_link_record_dynamic_symbol\nfor undefweak symbols, and add the missing code to generate RELATIVE reloc to\nR_RISCV_GOT entries in the relocate_section if a symbol is not dynamic and is\nnot undefined weak under pic and pie.\n\nPassed the gcc/binutils regressions of riscv-gnu-toolchain at least.\n",
  "tree_diff": [
    {
      "type": "modify",
      "old_id": "4689f3889275e59e9df3ede419dcfde5ba9d788f",
      "old_mode": 33188,
      "old_path": "bfd/elfnn-riscv.c",
      "new_id": "84f121c1b14e22699e0bc046a955032425f97aad",
      "new_mode": 33188,
      "new_path": "bfd/elfnn-riscv.c"
    },
    {
      "type": "add",
      "old_id": "0000000000000000000000000000000000000000",
      "old_mode": 0,
      "old_path": "/dev/null",
      "new_id": "7bdb16b197d406694195c2ac4e6f78ba5497950b",
      "new_mode": 33188,
      "new_path": "ld/testsuite/ld-riscv-elf/discard-exe.d"
    },
    {
      "type": "add",
      "old_id": "0000000000000000000000000000000000000000",
      "old_mode": 0,
      "old_path": "/dev/null",
      "new_id": "9ac2cc6850f6443e571f3839a792f0d0693f9325",
      "new_mode": 33188,
      "new_path": "ld/testsuite/ld-riscv-elf/discard-pic.d"
    },
    {
      "type": "add",
      "old_id": "0000000000000000000000000000000000000000",
      "old_mode": 0,
      "old_path": "/dev/null",
      "new_id": "cb95c4a9ffb1dd5d4b608a0791aadf34f59eea11",
      "new_mode": 33188,
      "new_path": "ld/testsuite/ld-riscv-elf/discard-pie.d"
    },
    {
      "type": "add",
      "old_id": "0000000000000000000000000000000000000000",
      "old_mode": 0,
      "old_path": "/dev/null",
      "new_id": "3afed216fbc701e478b7a1fc5cf0e82a70d550f1",
      "new_mode": 33188,
      "new_path": "ld/testsuite/ld-riscv-elf/discard.ld"
    },
    {
      "type": "add",
      "old_id": "0000000000000000000000000000000000000000",
      "old_mode": 0,
      "old_path": "/dev/null",
      "new_id": "8dd15a83906fcd5ab8f6dc879889e4ecf065bf1b",
      "new_mode": 33188,
      "new_path": "ld/testsuite/ld-riscv-elf/discard.s"
    },
    {
      "type": "modify",
      "old_id": "70c9aa7f66adce9d3b73ab5834b29ec013792018",
      "old_mode": 33188,
      "old_path": "ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp",
      "new_id": "bae1105cad6a081a5dcb739bc994aca430095643",
      "new_mode": 33188,
      "new_path": "ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp"
    }
  ]
}
