It can happen that .tbss has a VMA which is after the last VMA of the PT_LOAD segment it is in, for example due to alignment requirements. This will cause ld to report an error "`.tbss' can't be allocated in segment". But the check is actually bogus, because the VMA of .tbss is never used. As long as .tbss firs in the PT_TLS segment, all is well. There is already an exception in the macro ELF_SECTION_SIZE, which says that the size of .tbss should not be considered when the segment is PT_LOAD. Well, neither should the VMA. My suggested fix is the following, which leverages the already implemented special handling for .tbss: diff --git a/include/elf/internal.h b/include/elf/internal.h index 9ea175c..0ce9385 100644 --- a/include/elf/internal.h +++ b/include/elf/internal.h @@ -316,6 +316,7 @@ struct elf_segment_map /* SHF_ALLOC sections must have VMAs within the segment. Be careful about segments right at the end of memory. */ \ && ((sec_hdr->sh_flags & SHF_ALLOC) == 0 \ + || ELF_SECTION_SIZE(sec_hdr,segment) == 0 \ || (sec_hdr->sh_addr >= segment->p_vaddr && (sec_hdr->sh_addr - segment->p_vaddr \ + ELF_SECTION_SIZE(sec_hdr, segment) <= segment->p_memsz)))) The rationale here is that if the section has zero size (either becase it's actually empty, or because we are ignoring the size because it's .tbbs in PT_LOAD), then the VMA does not matter, because the address space is empty anyway. The bug exists in both 2.20.1 and 2.21.
Hi Marcus, Please could you create a small test case to reproduce this problem ? Looking at the current definition of ELF_SECTION_IN_SEGMENT I suspect that the problem might have already been resolved. Cheers Nick
I think the patch in comment #1 will cause problems. If VMA match for .tbss is disabled then ELF_SECTION_IN_SEGMENT will allow .tbss in any and all PT_LOAD segments.
@Nick: Thank you for looking at this. My test case is the following program: static __thread char baz=17; static __thread char foo[1024] __attribute__((aligned(1024))); int main() { return 0; } Compiling this (without optimization) yields: Sections: Idx Name Size VMA LMA File off Algn [...] 17 .tdata 00000001 0000000000032000 0000000000032000 00022000 2**10 CONTENTS, ALLOC, LOAD, DATA, THREAD_LOCAL 18 .tbss 00000b80 0000000000032400 0000000000032400 00022001 2**10 ALLOC, THREAD_LOCAL [...] and Program Header: [...] LOAD off 0x0000000000020000 vaddr 0x0000000000030000 paddr 0x0000000000030000 align 2**16 filesz 0x0000000000002001 memsz 0x0000000000002001 flags rw- [...] ld complains because 32400 > 30000+2001. @Alan: Indeed it will allow that. And that would be a problem... why exactly? .tbss is never loaded in the traditional sense; since it has no loading requirements its loading requirements (none) are fulfilled by any and all PT_LOAD segments, and so it is "in" them in the same way that the empty set is a subset of any set.
I was thinking that it would make readelf -l display the .tbss section as belonging to all load segments, but that isn't the case, so s/will/might/ in my previous comment. I'm still concerned that changing ELF_SECTION_IN_SEGMENT like this will break objcopy
Heh, current objcopy/strip breaks on your testcase.
Ok. I'm afraid you might be right about breakage in objcopy with the patch; I tried strip and the resulting binary had incorrect file size for PT_TLS. :-( Although here I blame the rather dubious calulcation of file size for non-load segments, which indeed is the only reason why .tbss needs to go into a load segment in the first place: Since .tbss (or any SHT_NOBITS section for that matter) does not actually exist in the file, nothing ought to rely on it having a filepos...
(In reply to comment #3) > @Nick: > Thank you for looking at this. My test case is the following program: > > static __thread char baz=17; > static __thread char foo[1024] __attribute__((aligned(1024))); > int main() { return 0; } > > Compiling this (without optimization) yields: > It works fine on Linux/x86. What is your target?
The target in my case is PS3/powerpc64. I can provide the linker script if necessary.
(In reply to comment #8) > The target in my case is PS3/powerpc64. I can provide the linker script if > necessary. Do you have a testcase for Linux/x86?
No, I don't have Linux/x86 at all.
CVSROOT: /cvs/src Module name: src Changes by: amodra@sourceware.org 2011-05-20 15:32:25 Modified files: bfd : ChangeLog elf.c Log message: PR 12763 * elf.c (_bfd_elf_make_section_from_shdr): Set up TLS section LMAs from PT_TLS header. (_bfd_elf_map_sections_to_segments): Don't create a final PT_LOAD segment if just for .tbss. (assign_file_positions_for_load_sections): Don't report "can't allocate in segment" errors for .tbss. (assign_file_positions_for_non_load_sections): Don't set p_filesz from SHT_NOBITS section filepos. Patches: http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/ChangeLog.diff?cvsroot=src&r1=1.5351&r2=1.5352 http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf.c.diff?cvsroot=src&r1=1.536&r2=1.537
CVSROOT: /cvs/src Module name: src Changes by: amodra@sourceware.org 2011-05-20 15:33:30 Modified files: ld/testsuite : ChangeLog ld/testsuite/ld-elf: binutils.exp Added files: ld/testsuite/ld-elf: tbss3.s tdata3.s Log message: PR 12763 * ld-elf/tdata3.s: New test. * ld-elf/tbss3.s: New test. * ld-elf/binutils.exp: Consolidate tbss and tdata tests. Patches: http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ChangeLog.diff?cvsroot=src&r1=1.1409&r2=1.1410 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-elf/tbss3.s.diff?cvsroot=src&r1=NONE&r2=1.1 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-elf/tdata3.s.diff?cvsroot=src&r1=NONE&r2=1.1 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-elf/binutils.exp.diff?cvsroot=src&r1=1.13&r2=1.14
Thanks! I applied your changes to my 2.20.1 tree, and now both ld and strip work A-ok!
CVSROOT: /cvs/src Module name: src Changes by: amodra@sourceware.org 2011-05-23 05:29:38 Modified files: ld/testsuite : ChangeLog ld/testsuite/ld-elf: binutils.exp Log message: PR 12763 * ld-elf/binutils.exp: Don't run tdata3 for hppa64. Patches: http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ChangeLog.diff?cvsroot=src&r1=1.1411&r2=1.1412 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-elf/binutils.exp.diff?cvsroot=src&r1=1.14&r2=1.15
CVSROOT: /cvs/src Module name: src Changes by: amodra@sourceware.org 2011-05-23 05:41:01 Modified files: bfd : ChangeLog elf.c ld : ChangeLog ldlang.c Log message: PR 12763 bfd/ * elf.c (assign_file_positions_for_load_sections): Set sh_offset for .tbss, and page align same for all SHT_NOBITS sections. ld/ * ldlang.c (lang_output_section_find_by_flags): Match orphan .sdata2 like sections to existing .sdata2, and similarly for orphan TLS sections. * emultempl/elf32.em (place_orphan): Exclude .tbss from orphan_bss. Patches: http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/ChangeLog.diff?cvsroot=src&r1=1.5352&r2=1.5353 http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf.c.diff?cvsroot=src&r1=1.537&r2=1.538 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/ChangeLog.diff?cvsroot=src&r1=1.2336&r2=1.2337 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/ldlang.c.diff?cvsroot=src&r1=1.369&r2=1.370
CVSROOT: /cvs/src Module name: src Changes by: amodra@sourceware.org 2011-05-27 12:47:12 Modified files: ld/emultempl : elf32.em Log message: PR 12763 missed from last commit Patches: http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/emultempl/elf32.em.diff?cvsroot=src&r1=1.218&r2=1.219
CVSROOT: /cvs/src Module name: src Branch: binutils-2_21-branch Changes by: amodra@sourceware.org 2011-05-29 04:51:53 Modified files: bfd : ChangeLog aoutx.h archive.c bfd-in2.h bfdio.c coff-aux.c coffcode.h coffgen.c cofflink.c config.in configure configure.in dwarf2.c elf-bfd.h elf-m10300.c elf.c elf32-ppc.c elf32-rx.c elf64-ppc.c elflink.c elfxx-ia64.c libbfd.c linker.c peXXigen.c vms-alpha.c bfd/hosts : x86-64linux.h binutils : ChangeLog nm.c objcopy.c binutils/doc : binutils.texi binutils/testsuite: ChangeLog binutils/testsuite/binutils-all: nm.exp binutils/testsuite/lib: utils-lib.exp gas : ChangeLog dwarf2dbg.c input-scrub.c messages.c read.c gas/config : obj-elf.h tc-arc.c tc-d10v.h tc-d30v.h tc-m32r.h gas/testsuite : ChangeLog gas/testsuite/gas/i386: inval-equ-2.l gas/testsuite/gas/symver: symver2.l include : ChangeLog ansidecl.h bfdlink.h ld : ChangeLog ldexp.c ldlang.c ldlex.l ldmain.c plugin.c ld/emultempl : elf32.em pe.em pep.em ld/scripttempl : pe.sc pep.sc ld/testsuite : ChangeLog ld/testsuite/ld-cris: tls-e-tpoffcomm1.d ld/testsuite/ld-plugin: plugin-7.d plugin-8.d Log message: PR 12365 PR 12613 PR 12632 PR 12739 PR 12753 PR 12760 PR 12763 Apply fix from mainline along with assorted other small fixes. Patches: http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/ChangeLog.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.5180.2.34&r2=1.5180.2.35 http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/aoutx.h.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.84.2.1&r2=1.84.2.2 http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/archive.c.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.69.2.3&r2=1.69.2.4 http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/bfd-in2.h.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.522.2.2&r2=1.522.2.3 http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/bfdio.c.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.27&r2=1.27.4.1 http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/coff-aux.c.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.10.10.1&r2=1.10.10.2 http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/coffcode.h.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.171&r2=1.171.2.1 http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/coffgen.c.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.78&r2=1.78.4.1 http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/cofflink.c.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.74.2.1&r2=1.74.2.2 http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/config.in.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.48&r2=1.48.4.1 http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/configure.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.339.2.4&r2=1.339.2.5 http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/configure.in.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.287.2.4&r2=1.287.2.5 http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/dwarf2.c.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.138&r2=1.138.2.1 http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf-bfd.h.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.313.2.2&r2=1.313.2.3 http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf-m10300.c.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.106&r2=1.106.2.1 http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf.c.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.524.2.2&r2=1.524.2.3 http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-ppc.c.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.290.2.3&r2=1.290.2.4 http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf32-rx.c.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.5.2.1&r2=1.5.2.2 http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elf64-ppc.c.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.339.2.10&r2=1.339.2.11 http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elflink.c.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.388.2.2&r2=1.388.2.3 http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/elfxx-ia64.c.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.228.2.1&r2=1.228.2.2 http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/libbfd.c.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.53.2.1&r2=1.53.2.2 http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/linker.c.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.77.2.2&r2=1.77.2.3 http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/peXXigen.c.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.67&r2=1.67.2.1 http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/vms-alpha.c.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.37.2.1&r2=1.37.2.2 http://sourceware.org/cgi-bin/cvsweb.cgi/src/bfd/hosts/x86-64linux.h.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.2&r2=1.2.4.1 http://sourceware.org/cgi-bin/cvsweb.cgi/src/binutils/ChangeLog.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.1708.2.9&r2=1.1708.2.10 http://sourceware.org/cgi-bin/cvsweb.cgi/src/binutils/nm.c.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.65&r2=1.65.2.1 http://sourceware.org/cgi-bin/cvsweb.cgi/src/binutils/objcopy.c.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.147.2.1&r2=1.147.2.2 http://sourceware.org/cgi-bin/cvsweb.cgi/src/binutils/doc/binutils.texi.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.173.2.1&r2=1.173.2.2 http://sourceware.org/cgi-bin/cvsweb.cgi/src/binutils/testsuite/ChangeLog.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.216.2.2&r2=1.216.2.3 http://sourceware.org/cgi-bin/cvsweb.cgi/src/binutils/testsuite/binutils-all/nm.exp.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.5&r2=1.5.4.1 http://sourceware.org/cgi-bin/cvsweb.cgi/src/binutils/testsuite/lib/utils-lib.exp.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.20.2.1&r2=1.20.2.2 http://sourceware.org/cgi-bin/cvsweb.cgi/src/gas/ChangeLog.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.4320.2.29&r2=1.4320.2.30 http://sourceware.org/cgi-bin/cvsweb.cgi/src/gas/dwarf2dbg.c.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.107&r2=1.107.2.1 http://sourceware.org/cgi-bin/cvsweb.cgi/src/gas/input-scrub.c.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.23&r2=1.23.2.1 http://sourceware.org/cgi-bin/cvsweb.cgi/src/gas/messages.c.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.23&r2=1.23.2.1 http://sourceware.org/cgi-bin/cvsweb.cgi/src/gas/read.c.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.167.2.2&r2=1.167.2.3 http://sourceware.org/cgi-bin/cvsweb.cgi/src/gas/config/obj-elf.h.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.38&r2=1.38.2.1 http://sourceware.org/cgi-bin/cvsweb.cgi/src/gas/config/tc-arc.c.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.47&r2=1.47.2.1 http://sourceware.org/cgi-bin/cvsweb.cgi/src/gas/config/tc-d10v.h.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.20&r2=1.20.2.1 http://sourceware.org/cgi-bin/cvsweb.cgi/src/gas/config/tc-d30v.h.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.15&r2=1.15.4.1 http://sourceware.org/cgi-bin/cvsweb.cgi/src/gas/config/tc-m32r.h.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.23&r2=1.23.4.1 http://sourceware.org/cgi-bin/cvsweb.cgi/src/gas/testsuite/ChangeLog.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.1802.2.11&r2=1.1802.2.12 http://sourceware.org/cgi-bin/cvsweb.cgi/src/gas/testsuite/gas/i386/inval-equ-2.l.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.1&r2=1.1.12.1 http://sourceware.org/cgi-bin/cvsweb.cgi/src/gas/testsuite/gas/symver/symver2.l.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.1&r2=1.1.38.1 http://sourceware.org/cgi-bin/cvsweb.cgi/src/include/ChangeLog.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.510.2.2&r2=1.510.2.3 http://sourceware.org/cgi-bin/cvsweb.cgi/src/include/ansidecl.h.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.28&r2=1.28.6.1 http://sourceware.org/cgi-bin/cvsweb.cgi/src/include/bfdlink.h.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.82.2.1&r2=1.82.2.2 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/ChangeLog.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.2222.2.23&r2=1.2222.2.24 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/ldexp.c.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.87.2.2&r2=1.87.2.3 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/ldlang.c.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.347.2.5&r2=1.347.2.6 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/ldlex.l.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.47.2.2&r2=1.47.2.3 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/ldmain.c.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.145.2.3&r2=1.145.2.4 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/plugin.c.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.7.2.5&r2=1.7.2.6 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/emultempl/elf32.em.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.214.2.1&r2=1.214.2.2 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/emultempl/pe.em.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.162.2.2&r2=1.162.2.3 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/emultempl/pep.em.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.38.2.1&r2=1.38.2.2 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/scripttempl/pe.sc.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.24&r2=1.24.2.1 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/scripttempl/pep.sc.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.13&r2=1.13.2.1 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ChangeLog.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.1322.2.12&r2=1.1322.2.13 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-cris/tls-e-tpoffcomm1.d.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.1&r2=1.1.6.1 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-plugin/plugin-7.d.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.1.2.1&r2=1.1.2.2 http://sourceware.org/cgi-bin/cvsweb.cgi/src/ld/testsuite/ld-plugin/plugin-8.d.diff?cvsroot=src&only_with_tag=binutils-2_21-branch&r1=1.1.2.1&r2=1.1.2.2
Fixed