[PATCH] D30843: ELF: Resolve _end symbols correctly.
Rafael Avila de Espindola via llvm-commits
llvm-commits at lists.llvm.org
Mon Mar 13 05:53:15 PDT 2017
LGTM
Peter Collingbourne via Phabricator <reviews at reviews.llvm.org> writes:
> pcc created this revision.
>
> Fix a bug introduced in r297313 which caused them to resolve to the end
> of the ELF header in PIEs and DSOs.
>
>
> https://reviews.llvm.org/D30843
>
> Files:
> lld/ELF/Writer.cpp
> lld/test/ELF/pre_init_fini_array_missing.s
>
>
> Index: lld/test/ELF/pre_init_fini_array_missing.s
> ===================================================================
> --- lld/test/ELF/pre_init_fini_array_missing.s
> +++ lld/test/ELF/pre_init_fini_array_missing.s
> @@ -1,6 +1,8 @@
> // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
> // RUN: ld.lld %t -o %t2
> // RUN: llvm-objdump -d %t2 | FileCheck %s
> +// RUN: ld.lld -pie %t -o %t3
> +// RUN: llvm-objdump -d %t3 | FileCheck --check-prefix=PIE %s
> // REQUIRES: x86
>
> .globl _start
> @@ -28,3 +30,14 @@
> // CHECK-NEXT: 20100f: e8 ec ef df ff callq -2101268
> // CHECK-NEXT: 201014: e8 e7 ef df ff callq -2101273
> // CHECK-NEXT: 201019: e8 e2 ef df ff callq -2101278
> +
> +// In position-independent binaries, they resolve to the image base.
> +
> +// PIE: Disassembly of section .text:
> +// PIE-NEXT: _start:
> +// PIE-NEXT: 1000: e8 fb ef ff ff callq -4101
> +// PIE-NEXT: 1005: e8 f6 ef ff ff callq -4106
> +// PIE-NEXT: 100a: e8 f1 ef ff ff callq -4111
> +// PIE-NEXT: 100f: e8 ec ef ff ff callq -4116
> +// PIE-NEXT: 1014: e8 e7 ef ff ff callq -4121
> +// PIE-NEXT: 1019: e8 e2 ef ff ff callq -4126
> Index: lld/ELF/Writer.cpp
> ===================================================================
> --- lld/ELF/Writer.cpp
> +++ lld/ELF/Writer.cpp
> @@ -1203,10 +1203,15 @@
> auto Define = [&](StringRef Start, StringRef End, OutputSection *OS) {
> // These symbols resolve to the image base if the section does not exist.
> // A special value -1 indicates end of the section.
> - if (!OS && Config->pic())
> - OS = Out::ElfHeader;
> - addOptionalRegular<ELFT>(Start, OS, 0);
> - addOptionalRegular<ELFT>(End, OS, OS ? -1 : 0);
> + if (OS) {
> + addOptionalRegular<ELFT>(Start, OS, 0);
> + addOptionalRegular<ELFT>(End, OS, -1);
> + } else {
> + if (Config->pic())
> + OS = Out::ElfHeader;
> + addOptionalRegular<ELFT>(Start, OS, 0);
> + addOptionalRegular<ELFT>(End, OS, 0);
> + }
> };
>
> Define("__preinit_array_start", "__preinit_array_end", Out::PreinitArray);
>
>
> Index: lld/test/ELF/pre_init_fini_array_missing.s
> ===================================================================
> --- lld/test/ELF/pre_init_fini_array_missing.s
> +++ lld/test/ELF/pre_init_fini_array_missing.s
> @@ -1,6 +1,8 @@
> // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
> // RUN: ld.lld %t -o %t2
> // RUN: llvm-objdump -d %t2 | FileCheck %s
> +// RUN: ld.lld -pie %t -o %t3
> +// RUN: llvm-objdump -d %t3 | FileCheck --check-prefix=PIE %s
> // REQUIRES: x86
>
> .globl _start
> @@ -28,3 +30,14 @@
> // CHECK-NEXT: 20100f: e8 ec ef df ff callq -2101268
> // CHECK-NEXT: 201014: e8 e7 ef df ff callq -2101273
> // CHECK-NEXT: 201019: e8 e2 ef df ff callq -2101278
> +
> +// In position-independent binaries, they resolve to the image base.
> +
> +// PIE: Disassembly of section .text:
> +// PIE-NEXT: _start:
> +// PIE-NEXT: 1000: e8 fb ef ff ff callq -4101
> +// PIE-NEXT: 1005: e8 f6 ef ff ff callq -4106
> +// PIE-NEXT: 100a: e8 f1 ef ff ff callq -4111
> +// PIE-NEXT: 100f: e8 ec ef ff ff callq -4116
> +// PIE-NEXT: 1014: e8 e7 ef ff ff callq -4121
> +// PIE-NEXT: 1019: e8 e2 ef ff ff callq -4126
> Index: lld/ELF/Writer.cpp
> ===================================================================
> --- lld/ELF/Writer.cpp
> +++ lld/ELF/Writer.cpp
> @@ -1203,10 +1203,15 @@
> auto Define = [&](StringRef Start, StringRef End, OutputSection *OS) {
> // These symbols resolve to the image base if the section does not exist.
> // A special value -1 indicates end of the section.
> - if (!OS && Config->pic())
> - OS = Out::ElfHeader;
> - addOptionalRegular<ELFT>(Start, OS, 0);
> - addOptionalRegular<ELFT>(End, OS, OS ? -1 : 0);
> + if (OS) {
> + addOptionalRegular<ELFT>(Start, OS, 0);
> + addOptionalRegular<ELFT>(End, OS, -1);
> + } else {
> + if (Config->pic())
> + OS = Out::ElfHeader;
> + addOptionalRegular<ELFT>(Start, OS, 0);
> + addOptionalRegular<ELFT>(End, OS, 0);
> + }
> };
>
> Define("__preinit_array_start", "__preinit_array_end", Out::PreinitArray);
More information about the llvm-commits
mailing list