[PATCH] D31255: Force @{init, fini}_array if section name starts with ".{init, fini}_array.".
Rafael Avila de Espindola via llvm-commits
llvm-commits at lists.llvm.org
Wed Mar 22 17:49:42 PDT 2017
LGTM
Rui Ueyama via Phabricator <reviews at reviews.llvm.org> writes:
> ruiu updated this revision to Diff 92727.
> ruiu added a comment.
>
> - Fix typo.
>
>
> https://reviews.llvm.org/D31255
>
> Files:
> lld/ELF/InputSection.cpp
> lld/test/ELF/init-fini-progbits.s
>
>
> Index: lld/test/ELF/init-fini-progbits.s
> ===================================================================
> --- /dev/null
> +++ lld/test/ELF/init-fini-progbits.s
> @@ -0,0 +1,19 @@
> +// REQUIRES: x86
> +
> +// RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
> +// RUN: ld.lld %t -o %t.exe
> +// RUN: llvm-readobj -sections %t.exe | FileCheck %s
> +
> +// CHECK: Name: .init_array
> +// CHECK-NEXT: Type: SHT_INIT_ARRAY
> +// CHECK: Name: .fini_array
> +// CHECK-NEXT: Type: SHT_FINI_ARRAY
> +
> +.globl _start
> +_start:
> + nop
> +
> +.section .init_array.100, "aw", @progbits
> + .byte 0
> +.section .fini_array.100, "aw", @progbits
> + .byte 0
> Index: lld/ELF/InputSection.cpp
> ===================================================================
> --- lld/ELF/InputSection.cpp
> +++ lld/ELF/InputSection.cpp
> @@ -71,11 +71,30 @@
> this->Alignment = V;
> }
>
> +// GNU assembler 2.24 and LLVM 4.0.0's MC (the newest release as of
> +// March 2017) fail to infer section types for sections starting with
> +// ".init_array." or ".fini_array.". They set SHT_PROGBITS instead. As a
> +// result, the following assembler directive creates ".init_array.100"
> +// with SHT_PROGBITS, while it should be SHT_INIT_ARRAY, for example.
> +//
> +// .section .init_array.100, "aw"
> +//
> +// This function forces SHT_{INIT,FINI}_ARRAY so that we can handle
> +// incorrect inputs as if they were correct from the beginning.
> +static uint64_t getType(uint64_t Type, StringRef Name) {
> + if (Type == SHT_PROGBITS && Name.startswith(".init_array."))
> + return SHT_INIT_ARRAY;
> + if (Type == SHT_PROGBITS && Name.startswith(".fini_array."))
> + return SHT_FINI_ARRAY;
> + return Type;
> +}
> +
> template <class ELFT>
> InputSectionBase::InputSectionBase(elf::ObjectFile<ELFT> *File,
> const typename ELFT::Shdr *Hdr,
> StringRef Name, Kind SectionKind)
> - : InputSectionBase(File, Hdr->sh_flags & ~SHF_INFO_LINK, Hdr->sh_type,
> + : InputSectionBase(File, Hdr->sh_flags & ~SHF_INFO_LINK,
> + getType(Hdr->sh_type, Name),
> Hdr->sh_entsize, Hdr->sh_link, Hdr->sh_info,
> Hdr->sh_addralign, getSectionContents(File, Hdr), Name,
> SectionKind) {
>
>
> Index: lld/test/ELF/init-fini-progbits.s
> ===================================================================
> --- /dev/null
> +++ lld/test/ELF/init-fini-progbits.s
> @@ -0,0 +1,19 @@
> +// REQUIRES: x86
> +
> +// RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
> +// RUN: ld.lld %t -o %t.exe
> +// RUN: llvm-readobj -sections %t.exe | FileCheck %s
> +
> +// CHECK: Name: .init_array
> +// CHECK-NEXT: Type: SHT_INIT_ARRAY
> +// CHECK: Name: .fini_array
> +// CHECK-NEXT: Type: SHT_FINI_ARRAY
> +
> +.globl _start
> +_start:
> + nop
> +
> +.section .init_array.100, "aw", @progbits
> + .byte 0
> +.section .fini_array.100, "aw", @progbits
> + .byte 0
> Index: lld/ELF/InputSection.cpp
> ===================================================================
> --- lld/ELF/InputSection.cpp
> +++ lld/ELF/InputSection.cpp
> @@ -71,11 +71,30 @@
> this->Alignment = V;
> }
>
> +// GNU assembler 2.24 and LLVM 4.0.0's MC (the newest release as of
> +// March 2017) fail to infer section types for sections starting with
> +// ".init_array." or ".fini_array.". They set SHT_PROGBITS instead. As a
> +// result, the following assembler directive creates ".init_array.100"
> +// with SHT_PROGBITS, while it should be SHT_INIT_ARRAY, for example.
> +//
> +// .section .init_array.100, "aw"
> +//
> +// This function forces SHT_{INIT,FINI}_ARRAY so that we can handle
> +// incorrect inputs as if they were correct from the beginning.
> +static uint64_t getType(uint64_t Type, StringRef Name) {
> + if (Type == SHT_PROGBITS && Name.startswith(".init_array."))
> + return SHT_INIT_ARRAY;
> + if (Type == SHT_PROGBITS && Name.startswith(".fini_array."))
> + return SHT_FINI_ARRAY;
> + return Type;
> +}
> +
> template <class ELFT>
> InputSectionBase::InputSectionBase(elf::ObjectFile<ELFT> *File,
> const typename ELFT::Shdr *Hdr,
> StringRef Name, Kind SectionKind)
> - : InputSectionBase(File, Hdr->sh_flags & ~SHF_INFO_LINK, Hdr->sh_type,
> + : InputSectionBase(File, Hdr->sh_flags & ~SHF_INFO_LINK,
> + getType(Hdr->sh_type, Name),
> Hdr->sh_entsize, Hdr->sh_link, Hdr->sh_info,
> Hdr->sh_addralign, getSectionContents(File, Hdr), Name,
> SectionKind) {
More information about the llvm-commits
mailing list