Created attachment 8529 [details] The patch that is described in the comment. test.c: main(){ return 0; } CFLAGS="-fuse-ld=gold -Wl,--emit-relocs -Wl,--icf=safe" make test cc -fuse-ld=gold -Wl,--emit-relocs -Wl,--icf=safe test.c -o test /usr/bin/ld.gold: internal error in do_layout, at ../../gold/object.cc:1723 collect2: error: ld returned 1 exit status make: *** [test] Error 1 I'm attaching a patch that fixes this problem. It changes so that the relocation sections are not added until the second pass in do_layout(). I also changed the assert to what I believe is the intended behavior, it is more in line with the handling of the eh_frame section.
We never actually intended to support --gc-sections or --icf with -r links, but we don't do anything to disallow the combination, leading to the internal error. Gold should have some code in General_options::finalize() to disallow it. With --emit-relocs, however, there really is no reason to disallow the combination, so we should make it work. (Out of curiosity, what are you using --emit-relocs for? I'm not complaining -- hardly anyone uses the option, so it hasn't had much real-world testing.)
Right now there is a check for the -r/--gc-section/--icf combination in queue_initial_tasks(), however I think I saw a PR about it generating lots of error afterwards. Can't find it though. I don't think the patch I attached would end in an assert failing when using -r in combination with --gc-section or --icf. I'm using it to relocate a program when loading it on an embedded system. Why not an relocatable ELF is used would be due to legacy, however it works quite well to do it this way. The only drawback would be that most of the relocation entries are unnecessary.
IBMs FDPR tool http://www.haifa.ibm.com/projects/systems/cot/fdpr/ uses --emit-relocs. Which is why powerpc is probably the only target to fuss over emitting relocs that make sense on edited code, eg. we translate tls relocs as well as tls code sequences.