diff --git a/debuginfo-tests/CMakeLists.txt b/debuginfo-tests/CMakeLists.txt --- a/debuginfo-tests/CMakeLists.txt +++ b/debuginfo-tests/CMakeLists.txt @@ -18,6 +18,7 @@ count llvm-config llvm-objdump + llvm-dwarfutil check-gdb-llvm-support not ) diff --git a/debuginfo-tests/dwarflinker/Inputs/empty-main.cpp b/debuginfo-tests/dwarflinker/Inputs/empty-main.cpp new file mode 100644 --- /dev/null +++ b/debuginfo-tests/dwarflinker/Inputs/empty-main.cpp @@ -0,0 +1 @@ +int main(void) { return 0; } diff --git a/debuginfo-tests/dwarflinker/Inputs/full-main.cpp b/debuginfo-tests/dwarflinker/Inputs/full-main.cpp new file mode 100644 --- /dev/null +++ b/debuginfo-tests/dwarflinker/Inputs/full-main.cpp @@ -0,0 +1,13 @@ +#include +int mod1_f1(int); +int mod2_f1(const char *); +int mod1_not_used(int p1); +int mod2_not_used(int p1); +int main(void) { + int res = mod1_f1(1); + res += mod2_f1("hello"); + res += mod1_not_used(res); + res += mod2_not_used(res); + printf("\n %d", res); + return 0; +} diff --git a/debuginfo-tests/dwarflinker/Inputs/gc-debuginfo-main-lto.cpp b/debuginfo-tests/dwarflinker/Inputs/gc-debuginfo-main-lto.cpp new file mode 100644 --- /dev/null +++ b/debuginfo-tests/dwarflinker/Inputs/gc-debuginfo-main-lto.cpp @@ -0,0 +1,16 @@ +#include + +int mod1_f1(int); +int mod2_f1(const char *); + +int main(void) { + int v1 = mod1_f1(3); + int v2 = mod2_f1("hello"); + + for (int idx = 0; idx < 3; idx++) { + printf("%d", v1 + idx); + printf("%d", v2 + idx); + } + + return 0; +} diff --git a/debuginfo-tests/dwarflinker/Inputs/gc-debuginfo-main.c b/debuginfo-tests/dwarflinker/Inputs/gc-debuginfo-main.c new file mode 100644 --- /dev/null +++ b/debuginfo-tests/dwarflinker/Inputs/gc-debuginfo-main.c @@ -0,0 +1,16 @@ +#include + +int mod1_f1(int); +int mod2_f1(int); + +__attribute__((optnone)) void func(int p) { printf("\n%d", p); } + +__attribute__((optnone)) int main(void) { + int v1 = mod1_f1(3); + int v2 = mod2_f1(5); + + for (int idx = 0; idx < 3; idx++) + func(v1 * idx + v2); + + return 0; +} diff --git a/debuginfo-tests/dwarflinker/Inputs/gc-debuginfo-mod1-lto.cpp b/debuginfo-tests/dwarflinker/Inputs/gc-debuginfo-mod1-lto.cpp new file mode 100644 --- /dev/null +++ b/debuginfo-tests/dwarflinker/Inputs/gc-debuginfo-mod1-lto.cpp @@ -0,0 +1,3 @@ +__attribute__((optnone)) int mod1_f1(int p1) { return p1 + 10; } + +__attribute__((optnone)) int mod1_not_used(int p1) { return p1 + 100; } diff --git a/debuginfo-tests/dwarflinker/Inputs/gc-debuginfo-mod1.c b/debuginfo-tests/dwarflinker/Inputs/gc-debuginfo-mod1.c new file mode 100644 --- /dev/null +++ b/debuginfo-tests/dwarflinker/Inputs/gc-debuginfo-mod1.c @@ -0,0 +1,8 @@ +int mod1_f1(int p1) { return p1 + 10; } + +int mod1_not_used(int p1) { + int r = 0; + for (int i = 0; i < p1; i++) + r++; + return r; +} diff --git a/debuginfo-tests/dwarflinker/Inputs/gc-debuginfo-mod2-lto.cpp b/debuginfo-tests/dwarflinker/Inputs/gc-debuginfo-mod2-lto.cpp new file mode 100644 --- /dev/null +++ b/debuginfo-tests/dwarflinker/Inputs/gc-debuginfo-mod2-lto.cpp @@ -0,0 +1,17 @@ + +class S { +public: + __attribute__((optnone)) S(const char *str) { + l = 0; + while (!*str) + l++; + } + + __attribute__((optnone)) int Length() { return l; } + + int l = 0; +}; + +__attribute__((optnone)) int mod2_f1(const char *n) { return S(n).Length(); } + +__attribute__((optnone)) int mod2_not_used(int p1) { return p1 + 100; } diff --git a/debuginfo-tests/dwarflinker/Inputs/gc-debuginfo-mod2.c b/debuginfo-tests/dwarflinker/Inputs/gc-debuginfo-mod2.c new file mode 100644 --- /dev/null +++ b/debuginfo-tests/dwarflinker/Inputs/gc-debuginfo-mod2.c @@ -0,0 +1,10 @@ + +int mod2_f1(int p1) { return p1 + 10; } + +int mod2_not_used(int p1) { + int r = 0; + for (int i = 0; i < p1; i++) + r++; + + return r; +} diff --git a/debuginfo-tests/dwarflinker/Inputs/odr-foo.cpp b/debuginfo-tests/dwarflinker/Inputs/odr-foo.cpp new file mode 100644 --- /dev/null +++ b/debuginfo-tests/dwarflinker/Inputs/odr-foo.cpp @@ -0,0 +1,3 @@ +#include "struct.h" + +__attribute__((optnone)) int foo(S p) { return p.a1 + p.a2; } diff --git a/debuginfo-tests/dwarflinker/Inputs/odr-main.cpp b/debuginfo-tests/dwarflinker/Inputs/odr-main.cpp new file mode 100644 --- /dev/null +++ b/debuginfo-tests/dwarflinker/Inputs/odr-main.cpp @@ -0,0 +1,10 @@ +#include "struct.h" +int foo(S); + +int main(void) { + S s; + s.a1 = 2; + s.a2 = 5; + + return foo(s); +} diff --git a/debuginfo-tests/dwarflinker/Inputs/struct.h b/debuginfo-tests/dwarflinker/Inputs/struct.h new file mode 100644 --- /dev/null +++ b/debuginfo-tests/dwarflinker/Inputs/struct.h @@ -0,0 +1,4 @@ +struct S { + int a1; + int a2; +}; diff --git a/debuginfo-tests/dwarflinker/full.cpp b/debuginfo-tests/dwarflinker/full.cpp new file mode 100644 --- /dev/null +++ b/debuginfo-tests/dwarflinker/full.cpp @@ -0,0 +1,138 @@ +// RUN: %clang %target_itanium_abi_host_triple -gdwarf-4 -ffunction-sections \ +// RUN: -O %p/Inputs/full-main.cpp -c -o %t.o +// RUN: %clang %target_itanium_abi_host_triple -gdwarf-4 -ffunction-sections \ +// RUN: -O %p/Inputs/gc-debuginfo-mod1-lto.cpp -c -o %t1.o +// RUN: %clang %target_itanium_abi_host_triple -gdwarf-4 -ffunction-sections \ +// RUN: -O %p/Inputs/gc-debuginfo-mod2-lto.cpp -c -o %t2.o +// RUN: %clang %target_itanium_abi_host_triple -gdwarf-4 -O -fuse-ld=lld \ +// RUN: -Wl,--gc-sections %t.o %t1.o %t2.o -o %t3.out +// RUN: llvm-dwarfutil --tombstone=bfd --strip-unoptimized-debug --gc \ +// RUN: --no-odr=0 %t3.out -o %t4.out +// RUN: llvm-dwarfdump -a %t4.out | FileCheck %s +// RUN: llvm-dwarfdump --verify %t4.out | FileCheck %s --check-prefix=VERIFY + +// UNSUPPORTED: system-darwin + +// This test checks that --gc-debuginfo does not remove debug info. +// (Because there is no code deleted by --gc-sections). +// It checks that debug info related to all functions from +// gc-debuginfo-mod1-lto.cpp and gc-debuginfo-mod2-lto.cpp is put +// into resulting binary + +CHECK: .debug_info contents: +CHECK: DW_TAG_compile_unit +CHECK: DW_AT_name ("{{.*}}full-main.cpp") +CHECK: DW_AT_stmt_list (0x[[MAIN_STMT_LIST:[0-9A-Fa-f]+]]) +CHECK: DW_AT_low_pc (0x[[LOWPC_ADDR:[0-9A-Fa-f]+]]) +CHECK: DW_AT_high_pc (0x[[HIGHPC_ADDR:[0-9A-Fa-f]+]]) +CHECK: DW_TAG_subprogram +CHECK: DW_AT_low_pc (0x[[LOWPC_ADDR]]) +CHECK: DW_AT_high_pc (0x[[HIGHPC_ADDR]]) +CHECK: DW_AT_name ("main") +CHECK: DW_TAG_variable +CHECK: DW_AT_location +CHECK: DW_AT_name ("res") +CHECK: DW_TAG_subprogram +CHECK: DW_AT_name ("mod1_f1") +CHECK: DW_TAG_formal_parameter +CHECK: DW_TAG_subprogram +CHECK: DW_AT_name ("mod2_f1") +CHECK: DW_TAG_formal_parameter +CHECK: DW_TAG_subprogram +CHECK: DW_AT_name ("mod1_not_used") +CHECK: DW_TAG_formal_parameter +CHECK: DW_TAG_subprogram +CHECK: DW_AT_name ("mod2_not_used") +CHECK: DW_TAG_formal_parameter +CHECK: NULL +CHECK: NULL + +CHECK: DW_TAG_compile_unit +CHECK: DW_AT_name ("{{.*}}gc-debuginfo-mod1-lto.cpp") +CHECK: DW_AT_stmt_list (0x[[MOD1_STMT_LIST:[0-9A-Fa-f]+]]) +CHECK: DW_AT_ranges +CHECK: [0x{{0*}}[[MOD1_ADDR:[0-9A-Fa-f]+]], +CHECK: [0x{{0*}}[[MOD1_NON_USED_ADDR:[0-9A-Fa-f]+]], +CHECK: DW_TAG_subprogram +CHECK: DW_AT_low_pc (0x{{0*}}[[MOD1_ADDR]] +CHECK: DW_AT_name ("mod1_f1") +CHECK: DW_TAG_subprogram +CHECK: DW_AT_low_pc (0x{{0*}}[[MOD1_NON_USED_ADDR]] +CHECK: DW_AT_name ("mod1_not_used") +CHECK: NULL +CHECK: NULL + +CHECK: DW_TAG_compile_unit +CHECK: DW_AT_name ("{{.*}}gc-debuginfo-mod2-lto.cpp") +CHECK: DW_AT_stmt_list (0x[[MOD2_STMT_LIST:[0-9A-Fa-f]+]]) +CHECK: DW_AT_ranges +CHECK: [0x{{0*}}[[MOD2_ADDR:[0-9A-Fa-f]+]], +CHECK: [0x{{0*}}[[MOD2_S_ADDR:[0-9A-Fa-f]+]], +CHECK: [0x{{0*}}[[MOD2_LENGTH_ADDR:[0-9A-Fa-f]+]], +CHECK: [0x{{0*}}[[MOD2_NON_USED_ADDR:[0-9A-Fa-f]+]], +CHECK: DW_TAG_class_type +CHECK: DW_AT_name ("S") +CHECK: DW_TAG_member +CHECK: DW_AT_name ("l") +CHECK: DW_TAG_subprogram +CHECK: DW_AT_name ("S") +CHECK: DW_TAG_subprogram +CHECK: DW_AT_name ("Length") +CHECK: NULL +CHECK: NULL +CHECK: DW_TAG_subprogram +CHECK: DW_AT_low_pc (0x{{0*}}[[MOD2_ADDR]] +CHECK: DW_AT_name ("mod2_f1") +CHECK: DW_TAG_formal_parameter +CHECK: DW_AT_name ("n") +CHECK: DW_TAG_subprogram +CHECK: DW_AT_low_pc (0x{{0*}}[[MOD2_S_ADDR]] +CHECK: DW_TAG_formal_parameter +CHECK: DW_AT_name ("this") +CHECK: DW_TAG_formal_parameter +CHECK: DW_AT_name ("str") +CHECK: DW_TAG_subprogram +CHECK: DW_AT_low_pc (0x{{0*}}[[MOD2_LENGTH_ADDR]] +CHECK: DW_TAG_formal_parameter +CHECK: DW_AT_name ("this") +CHECK: DW_TAG_subprogram +CHECK: DW_AT_low_pc (0x{{0*}}[[MOD2_NON_USED_ADDR]] +CHECK: DW_TAG_formal_parameter +CHECK: DW_AT_name ("p1") +CHECK: NULL + +CHECK: .debug_loc contents: + +CHECK: .eh_frame contents: + +CHECK: .debug_line contents: +CHECK: debug_line[0x[[MAIN_STMT_LIST]]] +CHECK: Address +CHECK: 0x{{0*}}[[LOWPC_ADDR]] +CHECK: debug_line[0x[[MOD1_STMT_LIST]]] +CHECK: Address +CHECK: 0x{{0*}}[[MOD1_ADDR]] +CHECK: 0x{{0*}}[[MOD1_NON_USED_ADDR]] +CHECK: debug_line[0x[[MOD2_STMT_LIST]]] +CHECK: Address +CHECK: 0x{{0*}}[[MOD2_ADDR]] +CHECK: 0x{{0*}}[[MOD2_S_ADDR]] +CHECK: 0x{{0*}}[[MOD2_LENGTH_ADDR]] +CHECK: 0x{{0*}}[[MOD2_NON_USED_ADDR]] + +CHECK: .debug_str contents: +CHECK: "main" +CHECK: "res" +CHECK: "mod1_f1" +CHECK: "mod2_f1" +CHECK: "mod1_not_used" +CHECK: "mod2_not_used" +CHECK: "S" +CHECK: "Length" +CHECK: "this" + +CHECK: .debug_ranges contents: + + +VERIFY: Verifying +VERIFY: No errors. diff --git a/debuginfo-tests/dwarflinker/gc-debuginfo.c b/debuginfo-tests/dwarflinker/gc-debuginfo.c new file mode 100644 --- /dev/null +++ b/debuginfo-tests/dwarflinker/gc-debuginfo.c @@ -0,0 +1,98 @@ +// RUN: %clang %target_itanium_abi_host_triple -gdwarf-4 -ffunction-sections \ +// RUN: -fno-inline -O %p/Inputs/gc-debuginfo-main.c -c -o %t.o +// RUN: %clang %target_itanium_abi_host_triple -gdwarf-4 -ffunction-sections \ +// RUN: -fno-inline -O %p/Inputs/gc-debuginfo-mod1.c -c -o %t1.o +// RUN: %clang %target_itanium_abi_host_triple -gdwarf-4 -ffunction-sections \ +// RUN: -fno-inline -O %p/Inputs/gc-debuginfo-mod2.c -c -o %t2.o +// RUN: %clang %target_itanium_abi_host_triple -gdwarf-4 -O -fuse-ld=lld \ +// RUN: -Wl,--gc-sections %t.o %t1.o %t2.o -o %t3.out +// RUN: llvm-dwarfutil --tombstone=bfd --strip-unoptimized-debug --gc \ +// RUN: --no-odr=0 %t3.out -o %t4.out +// RUN: llvm-dwarfdump -a %t4.out | FileCheck %s +// RUN: llvm-dwarfdump --verify %t4.out | FileCheck %s --check-prefix=VERIFY + +// UNSUPPORTED: system-darwin + +// This test checks that --gc-debuginfo removes unused debug info. +// It checks that debug info related to mod1_not_used() and +// mod2_not_used() functions is deleted from resulting .debug* +// tables. .debug_loc table should be empty since mod1_f1() +// and mod2_f1() functions do not refer to .debug_loc. + +CHECK: .debug_info contents: +CHECK: DW_TAG_compile_unit +CHECK: DW_AT_name ("{{.*}}gc-debuginfo-main.c") +CHECK: DW_AT_stmt_list (0x[[MAIN_STMT_LIST:[0-9A-Fa-f]+]]) +CHECK: DW_AT_ranges (0x00000000 +CHECK-NEXT: [0x{{0*}}[[MAIN_ADDR:[0-9A-Fa-f]+]], 0x{{[0-9a-zA-Z]*}}) +CHECK: DW_TAG_subprogram +CHECK: DW_AT_low_pc (0x{{0*}}[[MAIN_ADDR]]) +CHECK-NOT: mod1_not_used +CHECK-NOT: mod2_not_used +CHECK: DW_AT_name ("main") +CHECK: DW_TAG_subprogram +CHECK-NOT: mod1_not_used +CHECK-NOT: mod2_not_used +CHECK: DW_AT_name ("mod1_f1") +CHECK: DW_TAG_subprogram +CHECK-NOT: mod1_not_used +CHECK-NOT: mod2_not_used +CHECK: DW_AT_name ("mod2_f1") +CHECK: Compile Unit +CHECK: DW_TAG_compile_unit +CHECK-NOT: mod1_not_used +CHECK-NOT: mod2_not_used +CHECK: DW_AT_name ("{{.*}}gc-debuginfo-mod1.c") +CHECK: DW_AT_stmt_list (0x{{0*}}[[MOD1_STMT_LIST:[0-9A-Fa-f]+]]) +CHECK: DW_AT_ranges (0x{{0*}}[[RANGE_LIST_MOD1:[0-9A-Fa-f]+]] +CHECK-NEXT: [0x{{0*}}[[GET_MOD1_ADDR:[0-9A-Fa-f]+]], 0x{{[0-9a-zA-Z]*}})) +CHECK: DW_TAG_subprogram +CHECK: DW_AT_low_pc (0x{{0*}}[[GET_MOD1_ADDR]] +CHECK-NOT: mod1_not_used +CHECK-NOT: mod2_not_used +CHECK: DW_AT_name ("mod1_f1") +CHECK: Compile Unit +CHECK: DW_TAG_compile_unit +CHECK-NOT: mod1_not_used +CHECK-NOT: mod2_not_used +CHECK: DW_AT_name ("{{.*}}gc-debuginfo-mod2.c") +CHECK: DW_AT_stmt_list (0x{{0*}}[[MOD2_STMT_LIST:[0-9A-Fa-f]+]]) +CHECK: DW_AT_ranges (0x{{0*}}[[RANGE_LIST_MOD2:[0-9A-Fa-f]+]] +CHECK-NEXT: [0x[[GET_MOD2_ADDR:[0-9A-Fa-f]+]], 0x{{[0-9a-zA-Z]*}})) +CHECK: DW_TAG_subprogram +CHECK: DW_AT_low_pc (0x{{0*}}[[GET_MOD2_ADDR]] +CHECK-NOT: mod1_not_used +CHECK-NOT: mod2_not_used +CHECK: DW_AT_name ("mod2_f1") +CHECK-NOT: mod1_not_used +CHECK-NOT: mod2_not_used +CHECK: NULL + +CHECK-NOT: .debug_loc contents: + +CHECK: .debug_line contents: +CHECK: debug_line[0x[[MAIN_STMT_LIST]]] +CHECK: Address +CHECK: 0x{{0*}}[[MAIN_ADDR:[0-9A-Fa-f]+]] +CHECK: debug_line[0x{{0*}}[[MOD1_STMT_LIST]]] +CHECK: Address +CHECK: 0x{{0*}}[[GET_MOD1_ADDR:[0-9A-Fa-f]+]] +CHECK: debug_line[0x{{0*}}[[MOD2_STMT_LIST]]] +CHECK: Address +CHECK: 0x{{0*}}[[GET_MOD2_ADDR:[0-9A-Fa-f]+]] + + +CHECK: .debug_str contents: +CHECK-NOT: mod1_not_used +CHECK-NOT: mod2_not_used + +CHECK: .debug_ranges contents: +CHECK: 00000000 0000000000000000 {{[0-9a-zA-Z]*}} +CHECK: 00000000 +CHECK: {{0*}}[[RANGE_LIST_MOD1]] 0000000000000000 {{[0-9a-zA-Z]*}} +CHECK: {{0*}}[[RANGE_LIST_MOD1]] +CHECK: {{0*}}[[RANGE_LIST_MOD2]] 0000000000000000 {{[0-9a-zA-Z]*}} +CHECK: {{0*}}[[RANGE_LIST_MOD2]] + +VERIFY: Verifying +VERIFY: No errors. diff --git a/debuginfo-tests/dwarflinker/odr.cpp b/debuginfo-tests/dwarflinker/odr.cpp new file mode 100644 --- /dev/null +++ b/debuginfo-tests/dwarflinker/odr.cpp @@ -0,0 +1,50 @@ +// RUN: %clang %target_itanium_abi_host_triple -gdwarf-4 -ffunction-sections \ +// RUN: -fno-inline -O %p/Inputs/odr-main.cpp -c -o %t.o +// RUN: %clang %target_itanium_abi_host_triple -gdwarf-4 -ffunction-sections \ +// RUN: -fno-inline -O %p/Inputs/odr-foo.cpp -c -o %t1.o +// RUN: %clang %target_itanium_abi_host_triple -gdwarf-4 -O -fuse-ld=lld \ +// RUN: -Wl,--gc-sections %t.o %t1.o -o %t2.out +// RUN: llvm-dwarfutil --tombstone=bfd --strip-unoptimized-debug -gc \ +// RUN: --no-odr=0 %t2.out -o %t3.out +// RUN: llvm-dwarfdump -a %t3.out | FileCheck %s +// RUN: llvm-dwarfdump --verify %t3.out | FileCheck %s --check-prefix=VERIFY + +// UNSUPPORTED: system-darwin + +// This test checks that --gc-debuginfo properly implements +// One Definition Rule. It checks that debug info related to struct S +// presented only once(in first compilation unit). And that second +// compilation unit references definition from the first compilation unit. + +CHECK: .debug_info contents: +CHECK: DW_TAG_compile_unit +CHECK: DW_AT_name ("{{.*}}odr-main.cpp") +CHECK: DW_TAG_subprogram +CHECK: DW_AT_name ("main") +CHECK: DW_TAG_variable +CHECK: DW_AT_name ("s") +CHECK: DW_AT_type (0x{{0*}}[[STRUCT_OFFSET:[0-9A-Fa-f]+]] +CHECK: NULL +CHECK: NULL +CHECK: DW_TAG_subprogram +CHECK: DW_AT_name ("foo") +CHECK: DW_TAG_formal_parameter +CHECK: DW_AT_type (0x{{0*}}[[STRUCT_OFFSET]] +CHECK: 0x{{0*}}[[STRUCT_OFFSET:[0-9A-Fa-f]+]]: DW_TAG_structure_type +CHECK: DW_AT_name ("S") +CHECK: DW_TAG_member +CHECK: DW_AT_name ("a1") +CHECK: DW_AT_name ("a2") +CHECK: NULL +CHECK: NULL +CHECK: DW_TAG_compile_unit +CHECK: DW_AT_name ("{{.*}}odr-foo.cpp") +CHECK: DW_TAG_subprogram +CHECK: DW_AT_name ("foo") +CHECK: DW_TAG_formal_parameter +CHECK: DW_AT_name ("p") +CHECK: DW_AT_type (0x{{0*}}[[STRUCT_OFFSET]] +CHECK: NULL + +VERIFY: Verifying +VERIFY: No errors. diff --git a/llvm/include/llvm/DWARFLinker/DWARFLinker.h b/llvm/include/llvm/DWARFLinker/DWARFLinker.h --- a/llvm/include/llvm/DWARFLinker/DWARFLinker.h +++ b/llvm/include/llvm/DWARFLinker/DWARFLinker.h @@ -23,6 +23,7 @@ /// The kind of accelerator tables we should emit. enum class AccelTableKind { + None, Apple, ///< .apple_names, .apple_namespaces, .apple_types, .apple_objc. Dwarf, ///< DWARF v5 .debug_names. Default, ///< Dwarf for DWARF5 or later, Apple otherwise. @@ -64,14 +65,19 @@ /// section. Reset current relocation pointer if neccessary. virtual bool hasValidRelocs(bool ResetRelocsPtr = true) = 0; - /// Checks that there is a relocation against .debug_info - /// table between \p StartOffset and \p NextOffset. - /// - /// This function must be called with offsets in strictly ascending - /// order because it never looks back at relocations it already 'went past'. - /// \returns true and sets Info.InDebugMap if it is the case. - virtual bool hasValidRelocationAt(uint64_t StartOffset, uint64_t EndOffset, - CompileUnit::DIEInfo &Info) = 0; + /// Checks that specified address attribute points into live address. + /// This function must be called with DIE`s offsets in strictly + /// ascending order. + virtual bool isLiveAddressAttribute(const DWARFDie &DIE, + dwarf::Attribute Attr, + CompileUnit::DIEInfo &Info) = 0; + + /// Checks that specified location address attribute points into + /// live address. This function must be called with DIE`s offsets + /// in strictly ascending order. + virtual bool isLiveLocationAttribute(const DWARFDie &DIE, + dwarf::Attribute Attr, + CompileUnit::DIEInfo &Info) = 0; /// Apply the valid relocations to the buffer \p Data, taking into /// account that Data is at \p BaseOffset in the debug_info section. @@ -86,6 +92,18 @@ /// which points to sections with code). virtual RangesTy &getValidAddressRanges() = 0; + virtual bool isInLiveAddressRange(uint64_t Address) { + RangesTy &AddressRanges = getValidAddressRanges(); + + auto Range = AddressRanges.lower_bound(Address); + if (Range != AddressRanges.begin() && + (Range == AddressRanges.end() || Range->first > Address)) + --Range; + + return (Range != AddressRanges.end() && Range->first <= Address && + Range->second.HighPC >= Address); + } + /// Erases all data. virtual void clear() = 0; }; @@ -347,6 +365,8 @@ /// Given a DIE, update its incompleteness based on whether the DIEs it /// references are incomplete. UpdateRefIncompleteness, + /// Given a DIE, mark it as ODR Canonical if applicable. + MarkODRCanonicalDie, }; /// This class represents an item in the work list. The type defines what kind @@ -441,6 +461,8 @@ const DwarfFile &File, SmallVectorImpl &Worklist); + void markODRCanonicalDie(const DWARFDie &Die, CompileUnit &CU); + /// \defgroup FindRootDIEs Find DIEs corresponding to Address map entries. /// /// @{ diff --git a/llvm/include/llvm/DWARFLinker/DWARFLinkerCompileUnit.h b/llvm/include/llvm/DWARFLinker/DWARFLinkerCompileUnit.h --- a/llvm/include/llvm/DWARFLinker/DWARFLinkerCompileUnit.h +++ b/llvm/include/llvm/DWARFLinker/DWARFLinkerCompileUnit.h @@ -74,6 +74,9 @@ /// Does DIE transitively refer an incomplete decl? bool Incomplete : 1; + + /// Is the ODR Canonical DIE(first definition). + bool IsODRCanonical : 1; }; CompileUnit(DWARFUnit &OrigUnit, unsigned ID, bool CanUseODR, @@ -151,6 +154,12 @@ /// reconstructed accelerator tables. void markEverythingAsKept(); + /// Mark ODR canonical DIE(the DIE which represents the first definition). + void markODRCanonicalDie(const DWARFDie &Die, CompileUnit::DIEInfo &Info); + + /// Check whether specified DIE could be ODR Canonical candidate. + bool isODRCanonicalCandidate(const DWARFDie &Die); + /// Compute the end offset for this unit. Must be called after the CU's DIEs /// have been cloned. \returns the next unit offset (which is also the /// current debug_info section size). @@ -251,6 +260,8 @@ void setLabelBegin(MCSymbol *S) { LabelBegin = S; } private: + bool inModuleScope(const DWARFDie &Die); + DWARFUnit &OrigUnit; unsigned ID; std::vector Info; ///< DIE info indexed by DIE index. diff --git a/llvm/include/llvm/DWARFLinker/DWARFLinkerDeclContext.h b/llvm/include/llvm/DWARFLinker/DWARFLinkerDeclContext.h --- a/llvm/include/llvm/DWARFLinker/DWARFLinkerDeclContext.h +++ b/llvm/include/llvm/DWARFLinker/DWARFLinkerDeclContext.h @@ -88,6 +88,9 @@ bool setLastSeenDIE(CompileUnit &U, const DWARFDie &Die); + bool hasCanonicalDie() const { return HasCanonicalDie; } + void setHasCanonicalDie() { HasCanonicalDie = true; } + uint32_t getCanonicalDIEOffset() const { return CanonicalDIEOffset; } void setCanonicalDIEOffset(uint32_t Offset) { CanonicalDIEOffset = Offset; } @@ -111,6 +114,7 @@ DWARFDie LastSeenDIE; uint32_t LastSeenCompileUnitID = 0; uint32_t CanonicalDIEOffset = 0; + bool HasCanonicalDie = false; }; /// This class gives a tree-like API to the DenseMap that stores the diff --git a/llvm/include/llvm/MC/MCAsmInfo.h b/llvm/include/llvm/MC/MCAsmInfo.h --- a/llvm/include/llvm/MC/MCAsmInfo.h +++ b/llvm/include/llvm/MC/MCAsmInfo.h @@ -636,6 +636,11 @@ return DwarfUsesRelocationsAcrossSections; } + void + setDwarfUseRelocationsAcrossSections(bool UsesRelocationsAcrossSections) { + DwarfUsesRelocationsAcrossSections = UsesRelocationsAcrossSections; + } + bool doDwarfFDESymbolsUseAbsDiff() const { return DwarfFDESymbolsUseAbsDiff; } bool useDwarfRegNumForCFI() const { return DwarfRegNumForCFI; } bool useParensForSymbolVariant() const { return UseParensForSymbolVariant; } diff --git a/llvm/include/llvm/MC/MCObjectFileInfo.h b/llvm/include/llvm/MC/MCObjectFileInfo.h --- a/llvm/include/llvm/MC/MCObjectFileInfo.h +++ b/llvm/include/llvm/MC/MCObjectFileInfo.h @@ -401,9 +401,7 @@ // XCOFF specific sections MCSection *getTOCBaseSection() const { return TOCBaseSection; } - MCSection *getEHFrameSection() { - return EHFrameSection; - } + MCSection *getEHFrameSection() const { return EHFrameSection; } enum Environment { IsMachO, IsELF, IsCOFF, IsWasm, IsXCOFF }; Environment getObjectFileType() const { return Env; } diff --git a/llvm/include/llvm/ObjectCopy/Buffer.h b/llvm/include/llvm/ObjectCopy/Buffer.h new file mode 100644 --- /dev/null +++ b/llvm/include/llvm/ObjectCopy/Buffer.h @@ -0,0 +1,68 @@ +//===- Buffer.h -------------------------------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_OBJECTCOPY_BUFFER_H +#define LLVM_OBJECTCOPY_BUFFER_H + +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/FileOutputBuffer.h" +#include "llvm/Support/MemoryBuffer.h" +#include + +namespace llvm { +namespace objectcopy { + +// The class Buffer abstracts out the common interface of FileOutputBuffer and +// WritableMemoryBuffer so that the hierarchy of Writers depends on this +// abstract interface and doesn't depend on a particular implementation. +// TODO: refactor the buffer classes in LLVM to enable us to use them here +// directly. +class Buffer { + StringRef Name; + +public: + virtual ~Buffer(); + virtual Error allocate(size_t Size) = 0; + virtual uint8_t *getBufferStart() = 0; + virtual Error commit() = 0; + + explicit Buffer(StringRef Name) : Name(Name) {} + StringRef getName() const { return Name; } +}; + +class FileBuffer : public Buffer { + std::unique_ptr Buf; + // Indicates that allocate(0) was called, and commit() should create or + // truncate a file instead of using a FileOutputBuffer. + bool EmptyFile = false; + +public: + Error allocate(size_t Size) override; + uint8_t *getBufferStart() override; + Error commit() override; + + explicit FileBuffer(StringRef FileName) : Buffer(FileName) {} +}; + +class MemBuffer : public Buffer { + std::unique_ptr Buf; + +public: + Error allocate(size_t Size) override; + uint8_t *getBufferStart() override; + Error commit() override; + + explicit MemBuffer(StringRef Name) : Buffer(Name) {} + + std::unique_ptr releaseMemoryBuffer(); +}; + +} // end namespace objectcopy +} // end namespace llvm + +#endif // LLVM_OBJECTCOPY_BUFFER_H diff --git a/llvm/include/llvm/ObjectCopy/CopyConfig.h b/llvm/include/llvm/ObjectCopy/CopyConfig.h new file mode 100644 --- /dev/null +++ b/llvm/include/llvm/ObjectCopy/CopyConfig.h @@ -0,0 +1,258 @@ +//===- CopyConfig.h -------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_OBJECTCOPY_COPYCONFIG_H +#define LLVM_OBJECTCOPY_COPYCONFIG_H + +#include "ELF/ELFConfig.h" +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/BitmaskEnum.h" +#include "llvm/ADT/DenseSet.h" +#include "llvm/ADT/Optional.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringMap.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Object/ELFTypes.h" +#include "llvm/Support/Allocator.h" +#include "llvm/Support/Error.h" +#include "llvm/Support/GlobPattern.h" +#include "llvm/Support/Regex.h" +// Necessary for llvm::DebugCompressionType::None +#include "llvm/Target/TargetOptions.h" +#include + +namespace llvm { +namespace objectcopy { + +enum class FileFormat { + Unspecified, + ELF, + Binary, + IHex, +}; + +// This type keeps track of the machine info for various architectures. This +// lets us map architecture names to ELF types and the e_machine value of the +// ELF file. +struct MachineInfo { + MachineInfo(uint16_t EM, uint8_t ABI, bool Is64, bool IsLittle) + : EMachine(EM), OSABI(ABI), Is64Bit(Is64), IsLittleEndian(IsLittle) {} + // Alternative constructor that defaults to NONE for OSABI. + MachineInfo(uint16_t EM, bool Is64, bool IsLittle) + : MachineInfo(EM, ELF::ELFOSABI_NONE, Is64, IsLittle) {} + // Default constructor for unset fields. + MachineInfo() : MachineInfo(0, 0, false, false) {} + uint16_t EMachine; + uint8_t OSABI; + bool Is64Bit; + bool IsLittleEndian; +}; + +// Flags set by --set-section-flags or --rename-section. Interpretation of these +// is format-specific and not all flags are meaningful for all object file +// formats. This is a bitmask; many section flags may be set. +enum SectionFlag { + SecNone = 0, + SecAlloc = 1 << 0, + SecLoad = 1 << 1, + SecNoload = 1 << 2, + SecReadonly = 1 << 3, + SecDebug = 1 << 4, + SecCode = 1 << 5, + SecData = 1 << 6, + SecRom = 1 << 7, + SecMerge = 1 << 8, + SecStrings = 1 << 9, + SecContents = 1 << 10, + SecShare = 1 << 11, + SecExclude = 1 << 12, + LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/SecExclude) +}; + +struct SectionRename { + StringRef OriginalName; + StringRef NewName; + Optional NewFlags; +}; + +struct SectionFlagsUpdate { + StringRef Name; + SectionFlag NewFlags; +}; + +enum class DiscardType { + None, // Default + All, // --discard-all (-x) + Locals, // --discard-locals (-X) +}; + +enum class MatchStyle { + Literal, // Default for symbols. + Wildcard, // Default for sections, or enabled with --wildcard (-w). + Regex, // Enabled with --regex. +}; + +class NameOrPattern { + StringRef Name; + // Regex is shared between multiple CopyConfig instances. + std::shared_ptr R; + std::shared_ptr G; + bool IsPositiveMatch = true; + + NameOrPattern(StringRef N) : Name(N) {} + NameOrPattern(std::shared_ptr R) : R(R) {} + NameOrPattern(std::shared_ptr G, bool IsPositiveMatch) + : G(G), IsPositiveMatch(IsPositiveMatch) {} + +public: + // ErrorCallback is used to handle recoverable errors. An Error returned + // by the callback aborts the parsing and is then returned by this function. + static Expected + create(StringRef Pattern, MatchStyle MS, + llvm::function_ref ErrorCallback); + + bool isPositiveMatch() const { return IsPositiveMatch; } + bool operator==(StringRef S) const { + return R ? R->match(S) : G ? G->match(S) : Name == S; + } + bool operator!=(StringRef S) const { return !operator==(S); } +}; + +// Matcher that checks symbol or section names against the command line flags +// provided for that option. +class NameMatcher { + std::vector PosMatchers; + std::vector NegMatchers; + +public: + Error addMatcher(Expected Matcher) { + if (!Matcher) + return Matcher.takeError(); + if (Matcher->isPositiveMatch()) + PosMatchers.push_back(std::move(*Matcher)); + else + NegMatchers.push_back(std::move(*Matcher)); + return Error::success(); + } + bool matches(StringRef S) const { + return is_contained(PosMatchers, S) && !is_contained(NegMatchers, S); + } + bool empty() const { return PosMatchers.empty() && NegMatchers.empty(); } +}; + +// Configuration for copying/stripping a single file. +struct CopyConfig { + // Format-specific options to be initialized lazily when needed. + Optional ELF; + + // Main input/output options + StringRef InputFilename; + FileFormat InputFormat = FileFormat::Unspecified; + StringRef OutputFilename; + FileFormat OutputFormat = FileFormat::Unspecified; + + // Only applicable when --output-format!=binary (e.g. elf64-x86-64). + Optional OutputArch; + + // Advanced options + StringRef AddGnuDebugLink; + // Cached gnu_debuglink's target CRC + uint32_t GnuDebugLinkCRC32; + StringRef BuildIdLinkDir; + Optional BuildIdLinkInput; + Optional BuildIdLinkOutput; + Optional ExtractPartition; + StringRef SplitDWO; + StringRef SymbolsPrefix; + StringRef AllocSectionsPrefix; + DiscardType DiscardMode = DiscardType::None; + Optional NewSymbolVisibility; + + // Repeated options + std::vector AddSection; + std::vector DumpSection; + std::vector SymbolsToAdd; + std::vector RPathToAdd; + DenseMap RPathsToUpdate; + DenseMap InstallNamesToUpdate; + DenseSet RPathsToRemove; + + // install-name-tool's id option + Optional SharedLibId; + + // Section matchers + NameMatcher KeepSection; + NameMatcher OnlySection; + NameMatcher ToRemove; + + // Symbol matchers + NameMatcher SymbolsToGlobalize; + NameMatcher SymbolsToKeep; + NameMatcher SymbolsToLocalize; + NameMatcher SymbolsToRemove; + NameMatcher UnneededSymbolsToRemove; + NameMatcher SymbolsToWeaken; + NameMatcher SymbolsToKeepGlobal; + + // Map options + StringMap SectionsToRename; + StringMap SetSectionAlignment; + StringMap SetSectionFlags; + StringMap SymbolsToRename; + StringMap NewDebugSections; + + // ELF entry point address expression. The input parameter is an entry point + // address in the input ELF file. The entry address in the output file is + // calculated with EntryExpr(input_address), when either --set-start or + // --change-start is used. + std::function EntryExpr; + + // Boolean options + bool AllowBrokenLinks = false; + bool DeterministicArchives = true; + bool ExtractDWO = false; + bool ExtractMainPartition = false; + bool KeepFileSymbols = false; + bool LocalizeHidden = false; + bool OnlyKeepDebug = false; + bool PreserveDates = false; + bool StripAll = false; + bool StripAllGNU = false; + bool StripDWO = false; + bool StripDebug = false; + bool StripNonAlloc = false; + bool StripSections = false; + bool StripSwiftSymbols = false; + bool StripUnneeded = false; + bool Weaken = false; + bool DecompressDebugSections = false; + DebugCompressionType CompressionType = DebugCompressionType::None; +}; + +LLVM_ATTRIBUTE_NORETURN void error(Twine Message); +LLVM_ATTRIBUTE_NORETURN void error(Error E); +LLVM_ATTRIBUTE_NORETURN void reportError(StringRef File, std::error_code EC); +LLVM_ATTRIBUTE_NORETURN void reportError(StringRef File, Error E); +ErrorSuccess reportWarning(Error E); + +// This is taken from llvm-readobj. +// [see here](llvm/tools/llvm-readobj/llvm-readobj.h:38) +template T unwrapOrError(Expected EO) { + if (EO) + return *EO; + std::string Buf; + raw_string_ostream OS(Buf); + logAllUnhandledErrors(EO.takeError(), OS); + OS.flush(); + error(Buf); +} + +} // namespace objectcopy +} // namespace llvm + +#endif // LLVM_OBJECTCOPY_COPYCONFIG_H diff --git a/llvm/include/llvm/ObjectCopy/ELF/ELFConfig.h b/llvm/include/llvm/ObjectCopy/ELF/ELFConfig.h new file mode 100644 --- /dev/null +++ b/llvm/include/llvm/ObjectCopy/ELF/ELFConfig.h @@ -0,0 +1,44 @@ +//===- ELFConfig.h ----------------------------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_OBJECTCOPY_ELF_ELFCONFIG_H +#define LLVM_OBJECTCOPY_ELF_ELFCONFIG_H + +#include "llvm/ADT/Optional.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Object/ELFTypes.h" +#include "llvm/Support/Error.h" +#include + +namespace llvm { +namespace objectcopy { +struct CopyConfig; + +namespace elf { + +struct NewSymbolInfo { + StringRef SymbolName; + StringRef SectionName; + uint64_t Value = 0; + uint8_t Type = ELF::STT_NOTYPE; + uint8_t Bind = ELF::STB_GLOBAL; + uint8_t Visibility = ELF::STV_DEFAULT; +}; + +struct ELFCopyConfig { + Optional NewSymbolVisibility; + std::vector SymbolsToAdd; +}; + +Expected parseConfig(const CopyConfig &Config); + +} // namespace elf +} // namespace objectcopy +} // namespace llvm + +#endif diff --git a/llvm/include/llvm/ObjectCopy/ELF/ELFObjcopy.h b/llvm/include/llvm/ObjectCopy/ELF/ELFObjcopy.h new file mode 100644 --- /dev/null +++ b/llvm/include/llvm/ObjectCopy/ELF/ELFObjcopy.h @@ -0,0 +1,36 @@ +//===- ELFObjcopy.h ---------------------------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_OBJECTCOPY_ELF_ELFOBJCOPY_H +#define LLVM_OBJECTCOPY_ELF_ELFOBJCOPY_H + +namespace llvm { +class Error; +class MemoryBuffer; + +namespace object { +class ELFObjectFileBase; +} // end namespace object + +namespace objectcopy { +struct CopyConfig; +class Buffer; + +namespace elf { +Error executeObjcopyOnIHex(const CopyConfig &Config, MemoryBuffer &In, + Buffer &Out); +Error executeObjcopyOnRawBinary(const CopyConfig &Config, MemoryBuffer &In, + Buffer &Out); +Error executeObjcopyOnBinary(const CopyConfig &Config, + object::ELFObjectFileBase &In, Buffer &Out); + +} // end namespace elf +} // end namespace objectcopy +} // end namespace llvm + +#endif // LLVM_OBJECTCOPY_ELF_ELFOBJCOPY_H diff --git a/llvm/include/llvm/ObjectCopy/ELF/Object.h b/llvm/include/llvm/ObjectCopy/ELF/Object.h new file mode 100644 --- /dev/null +++ b/llvm/include/llvm/ObjectCopy/ELF/Object.h @@ -0,0 +1,1092 @@ +//===- Object.h -------------------------------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_OBJECTCOPY_ELF_OBJECT_H +#define LLVM_OBJECTCOPY_ELF_OBJECT_H + +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/ADT/Twine.h" +#include "llvm/BinaryFormat/ELF.h" +#include "llvm/MC/StringTableBuilder.h" +#include "llvm/Object/ELFObjectFile.h" +#include "llvm/ObjectCopy/Buffer.h" +#include "llvm/ObjectCopy/CopyConfig.h" +#include "llvm/Support/Errc.h" +#include "llvm/Support/FileOutputBuffer.h" +#include +#include +#include +#include +#include +#include + +namespace llvm { +enum class DebugCompressionType; +namespace objectcopy { +namespace elf { + +class SectionBase; +class Section; +class OwnedDataSection; +class StringTableSection; +class SymbolTableSection; +class RelocationSection; +class DynamicRelocationSection; +class GnuDebugLinkSection; +class GroupSection; +class SectionIndexSection; +class CompressedSection; +class DecompressedSection; +class Segment; +class Object; +struct Symbol; + +class SectionTableRef { + MutableArrayRef> Sections; + +public: + using iterator = pointee_iterator *>; + + explicit SectionTableRef(MutableArrayRef> Secs) + : Sections(Secs) {} + SectionTableRef(const SectionTableRef &) = default; + + iterator begin() const { return iterator(Sections.data()); } + iterator end() const { return iterator(Sections.data() + Sections.size()); } + size_t size() const { return Sections.size(); } + + SectionBase *getSection(uint32_t Index, Twine ErrMsg); + + template + T *getSectionOfType(uint32_t Index, Twine IndexErrMsg, Twine TypeErrMsg); +}; + +enum ElfType { ELFT_ELF32LE, ELFT_ELF64LE, ELFT_ELF32BE, ELFT_ELF64BE }; + +class SectionVisitor { +public: + virtual ~SectionVisitor() = default; + + virtual void visit(const Section &Sec) = 0; + virtual void visit(const OwnedDataSection &Sec) = 0; + virtual void visit(const StringTableSection &Sec) = 0; + virtual void visit(const SymbolTableSection &Sec) = 0; + virtual void visit(const RelocationSection &Sec) = 0; + virtual void visit(const DynamicRelocationSection &Sec) = 0; + virtual void visit(const GnuDebugLinkSection &Sec) = 0; + virtual void visit(const GroupSection &Sec) = 0; + virtual void visit(const SectionIndexSection &Sec) = 0; + virtual void visit(const CompressedSection &Sec) = 0; + virtual void visit(const DecompressedSection &Sec) = 0; +}; + +class MutableSectionVisitor { +public: + virtual ~MutableSectionVisitor() = default; + + virtual void visit(Section &Sec) = 0; + virtual void visit(OwnedDataSection &Sec) = 0; + virtual void visit(StringTableSection &Sec) = 0; + virtual void visit(SymbolTableSection &Sec) = 0; + virtual void visit(RelocationSection &Sec) = 0; + virtual void visit(DynamicRelocationSection &Sec) = 0; + virtual void visit(GnuDebugLinkSection &Sec) = 0; + virtual void visit(GroupSection &Sec) = 0; + virtual void visit(SectionIndexSection &Sec) = 0; + virtual void visit(CompressedSection &Sec) = 0; + virtual void visit(DecompressedSection &Sec) = 0; +}; + +class SectionWriter : public SectionVisitor { +protected: + Buffer &Out; + +public: + virtual ~SectionWriter() = default; + + void visit(const Section &Sec) override; + void visit(const OwnedDataSection &Sec) override; + void visit(const StringTableSection &Sec) override; + void visit(const DynamicRelocationSection &Sec) override; + virtual void visit(const SymbolTableSection &Sec) override = 0; + virtual void visit(const RelocationSection &Sec) override = 0; + virtual void visit(const GnuDebugLinkSection &Sec) override = 0; + virtual void visit(const GroupSection &Sec) override = 0; + virtual void visit(const SectionIndexSection &Sec) override = 0; + virtual void visit(const CompressedSection &Sec) override = 0; + virtual void visit(const DecompressedSection &Sec) override = 0; + + explicit SectionWriter(Buffer &Buf) : Out(Buf) {} +}; + +template class ELFSectionWriter : public SectionWriter { +private: + using Elf_Word = typename ELFT::Word; + using Elf_Rel = typename ELFT::Rel; + using Elf_Rela = typename ELFT::Rela; + using Elf_Sym = typename ELFT::Sym; + +public: + virtual ~ELFSectionWriter() {} + void visit(const SymbolTableSection &Sec) override; + void visit(const RelocationSection &Sec) override; + void visit(const GnuDebugLinkSection &Sec) override; + void visit(const GroupSection &Sec) override; + void visit(const SectionIndexSection &Sec) override; + void visit(const CompressedSection &Sec) override; + void visit(const DecompressedSection &Sec) override; + + explicit ELFSectionWriter(Buffer &Buf) : SectionWriter(Buf) {} +}; + +template class ELFSectionSizer : public MutableSectionVisitor { +private: + using Elf_Rel = typename ELFT::Rel; + using Elf_Rela = typename ELFT::Rela; + using Elf_Sym = typename ELFT::Sym; + using Elf_Word = typename ELFT::Word; + using Elf_Xword = typename ELFT::Xword; + +public: + void visit(Section &Sec) override; + void visit(OwnedDataSection &Sec) override; + void visit(StringTableSection &Sec) override; + void visit(DynamicRelocationSection &Sec) override; + void visit(SymbolTableSection &Sec) override; + void visit(RelocationSection &Sec) override; + void visit(GnuDebugLinkSection &Sec) override; + void visit(GroupSection &Sec) override; + void visit(SectionIndexSection &Sec) override; + void visit(CompressedSection &Sec) override; + void visit(DecompressedSection &Sec) override; +}; + +#define MAKE_SEC_WRITER_FRIEND \ + friend class SectionWriter; \ + friend class IHexSectionWriterBase; \ + friend class IHexSectionWriter; \ + template friend class ELFSectionWriter; \ + template friend class ELFSectionSizer; + +class BinarySectionWriter : public SectionWriter { +public: + virtual ~BinarySectionWriter() {} + + void visit(const SymbolTableSection &Sec) override; + void visit(const RelocationSection &Sec) override; + void visit(const GnuDebugLinkSection &Sec) override; + void visit(const GroupSection &Sec) override; + void visit(const SectionIndexSection &Sec) override; + void visit(const CompressedSection &Sec) override; + void visit(const DecompressedSection &Sec) override; + + explicit BinarySectionWriter(Buffer &Buf) : SectionWriter(Buf) {} +}; + +using IHexLineData = SmallVector; + +struct IHexRecord { + // Memory address of the record. + uint16_t Addr; + // Record type (see below). + uint16_t Type; + // Record data in hexadecimal form. + StringRef HexData; + + // Helper method to get file length of the record + // including newline character + static size_t getLength(size_t DataSize) { + // :LLAAAATT[DD...DD]CC' + return DataSize * 2 + 11; + } + + // Gets length of line in a file (getLength + CRLF). + static size_t getLineLength(size_t DataSize) { + return getLength(DataSize) + 2; + } + + // Given type, address and data returns line which can + // be written to output file. + static IHexLineData getLine(uint8_t Type, uint16_t Addr, + ArrayRef Data); + + // Parses the line and returns record if possible. + // Line should be trimmed from whitespace characters. + static Expected parse(StringRef Line); + + // Calculates checksum of stringified record representation + // S must NOT contain leading ':' and trailing whitespace + // characters + static uint8_t getChecksum(StringRef S); + + enum Type { + // Contains data and a 16-bit starting address for the data. + // The byte count specifies number of data bytes in the record. + Data = 0, + // Must occur exactly once per file in the last line of the file. + // The data field is empty (thus byte count is 00) and the address + // field is typically 0000. + EndOfFile = 1, + // The data field contains a 16-bit segment base address (thus byte + // count is always 02) compatible with 80x86 real mode addressing. + // The address field (typically 0000) is ignored. The segment address + // from the most recent 02 record is multiplied by 16 and added to each + // subsequent data record address to form the physical starting address + // for the data. This allows addressing up to one megabyte of address + // space. + SegmentAddr = 2, + // or 80x86 processors, specifies the initial content of the CS:IP + // registers. The address field is 0000, the byte count is always 04, + // the first two data bytes are the CS value, the latter two are the + // IP value. + StartAddr80x86 = 3, + // Allows for 32 bit addressing (up to 4GiB). The record's address field + // is ignored (typically 0000) and its byte count is always 02. The two + // data bytes (big endian) specify the upper 16 bits of the 32 bit + // absolute address for all subsequent type 00 records + ExtendedAddr = 4, + // The address field is 0000 (not used) and the byte count is always 04. + // The four data bytes represent a 32-bit address value. In the case of + // 80386 and higher CPUs, this address is loaded into the EIP register. + StartAddr = 5, + // We have no other valid types + InvalidType = 6 + }; +}; + +// Base class for IHexSectionWriter. This class implements writing algorithm, +// but doesn't actually write records. It is used for output buffer size +// calculation in IHexWriter::finalize. +class IHexSectionWriterBase : public BinarySectionWriter { + // 20-bit segment address + uint32_t SegmentAddr = 0; + // Extended linear address + uint32_t BaseAddr = 0; + + // Write segment address corresponding to 'Addr' + uint64_t writeSegmentAddr(uint64_t Addr); + // Write extended linear (base) address corresponding to 'Addr' + uint64_t writeBaseAddr(uint64_t Addr); + +protected: + // Offset in the output buffer + uint64_t Offset = 0; + + void writeSection(const SectionBase *Sec, ArrayRef Data); + virtual void writeData(uint8_t Type, uint16_t Addr, ArrayRef Data); + +public: + explicit IHexSectionWriterBase(Buffer &Buf) : BinarySectionWriter(Buf) {} + + uint64_t getBufferOffset() const { return Offset; } + void visit(const Section &Sec) final; + void visit(const OwnedDataSection &Sec) final; + void visit(const StringTableSection &Sec) override; + void visit(const DynamicRelocationSection &Sec) final; + using BinarySectionWriter::visit; +}; + +// Real IHEX section writer +class IHexSectionWriter : public IHexSectionWriterBase { +public: + IHexSectionWriter(Buffer &Buf) : IHexSectionWriterBase(Buf) {} + + void writeData(uint8_t Type, uint16_t Addr, ArrayRef Data) override; + void visit(const StringTableSection &Sec) override; +}; + +class Writer { +protected: + Object &Obj; + Buffer &Buf; + +public: + virtual ~Writer(); + virtual Error finalize() = 0; + virtual Error write() = 0; + + Writer(Object &O, Buffer &B) : Obj(O), Buf(B) {} +}; + +template class ELFWriter : public Writer { +private: + using Elf_Addr = typename ELFT::Addr; + using Elf_Shdr = typename ELFT::Shdr; + using Elf_Phdr = typename ELFT::Phdr; + using Elf_Ehdr = typename ELFT::Ehdr; + + void initEhdrSegment(); + + void writeEhdr(); + void writePhdr(const Segment &Seg); + void writeShdr(const SectionBase &Sec); + + void writePhdrs(); + void writeShdrs(); + void writeSectionData(); + void writeSegmentData(); + + void assignOffsets(); + + std::unique_ptr> SecWriter; + + size_t totalSize() const; + +public: + virtual ~ELFWriter() {} + bool WriteSectionHeaders; + + // For --only-keep-debug, select an alternative section/segment layout + // algorithm. + bool OnlyKeepDebug; + + Error finalize() override; + Error write() override; + ELFWriter(Object &Obj, Buffer &Buf, bool WSH, bool OnlyKeepDebug); +}; + +class BinaryWriter : public Writer { +private: + std::unique_ptr SecWriter; + + uint64_t TotalSize = 0; + +public: + ~BinaryWriter() {} + Error finalize() override; + Error write() override; + BinaryWriter(Object &Obj, Buffer &Buf) : Writer(Obj, Buf) {} +}; + +class IHexWriter : public Writer { + struct SectionCompare { + bool operator()(const SectionBase *Lhs, const SectionBase *Rhs) const; + }; + + std::set Sections; + size_t TotalSize = 0; + + Error checkSection(const SectionBase &Sec); + uint64_t writeEntryPointRecord(uint8_t *Buf); + uint64_t writeEndOfFileRecord(uint8_t *Buf); + +public: + ~IHexWriter() {} + Error finalize() override; + Error write() override; + IHexWriter(Object &Obj, Buffer &Buf) : Writer(Obj, Buf) {} +}; + +class SectionBase { +public: + std::string Name; + Segment *ParentSegment = nullptr; + uint64_t HeaderOffset = 0; + uint32_t Index = 0; + bool HasSymbol = false; + + uint64_t OriginalFlags = 0; + uint64_t OriginalType = ELF::SHT_NULL; + uint64_t OriginalOffset = std::numeric_limits::max(); + + uint64_t Addr = 0; + uint64_t Align = 1; + uint32_t EntrySize = 0; + uint64_t Flags = 0; + uint64_t Info = 0; + uint64_t Link = ELF::SHN_UNDEF; + uint64_t NameIndex = 0; + uint64_t Offset = 0; + uint64_t Size = 0; + uint64_t Type = ELF::SHT_NULL; + ArrayRef OriginalData; + + SectionBase() = default; + SectionBase(const SectionBase &) = default; + + virtual ~SectionBase() = default; + + virtual void initialize(SectionTableRef SecTable); + virtual void finalize(); + // Remove references to these sections. The list of sections must be sorted. + virtual Error + removeSectionReferences(bool AllowBrokenLinks, + function_ref ToRemove); + virtual Error removeSymbols(function_ref ToRemove); + virtual void accept(SectionVisitor &Visitor) const = 0; + virtual void accept(MutableSectionVisitor &Visitor) = 0; + virtual void markSymbols(); + virtual void + replaceSectionReferences(const DenseMap &); + // Notify the section that it is subject to removal. + virtual void onRemove(); +}; + +class Segment { +private: + struct SectionCompare { + bool operator()(const SectionBase *Lhs, const SectionBase *Rhs) const { + // Some sections might have the same address if one of them is empty. To + // fix this we can use the lexicographic ordering on ->Addr and the + // address of the actully stored section. + if (Lhs->OriginalOffset == Rhs->OriginalOffset) + return Lhs < Rhs; + return Lhs->OriginalOffset < Rhs->OriginalOffset; + } + }; + +public: + uint32_t Type = 0; + uint32_t Flags = 0; + uint64_t Offset = 0; + uint64_t VAddr = 0; + uint64_t PAddr = 0; + uint64_t FileSize = 0; + uint64_t MemSize = 0; + uint64_t Align = 0; + + uint32_t Index = 0; + uint64_t OriginalOffset = 0; + Segment *ParentSegment = nullptr; + ArrayRef Contents; + std::set Sections; + + explicit Segment(ArrayRef Data) : Contents(Data) {} + Segment() = default; + + const SectionBase *firstSection() const { + if (!Sections.empty()) + return *Sections.begin(); + return nullptr; + } + + void removeSection(const SectionBase *Sec) { Sections.erase(Sec); } + void addSection(const SectionBase *Sec) { Sections.insert(Sec); } + + ArrayRef getContents() const { return Contents; } +}; + +class Section : public SectionBase { + MAKE_SEC_WRITER_FRIEND + + ArrayRef Contents; + SectionBase *LinkSection = nullptr; + +public: + explicit Section(ArrayRef Data) : Contents(Data) {} + + void accept(SectionVisitor &Visitor) const override; + void accept(MutableSectionVisitor &Visitor) override; + Error removeSectionReferences( + bool AllowBrokenLinks, + function_ref ToRemove) override; + void initialize(SectionTableRef SecTable) override; + void finalize() override; +}; + +class OwnedDataSection : public SectionBase { + MAKE_SEC_WRITER_FRIEND + + std::vector Data; + +public: + OwnedDataSection(StringRef SecName, ArrayRef Data) + : Data(std::begin(Data), std::end(Data)) { + Name = SecName.str(); + Type = OriginalType = ELF::SHT_PROGBITS; + Size = Data.size(); + OriginalOffset = std::numeric_limits::max(); + } + + OwnedDataSection(const Twine &SecName, uint64_t SecAddr, uint64_t SecFlags, + uint64_t SecOff) { + Name = SecName.str(); + Type = OriginalType = ELF::SHT_PROGBITS; + Addr = SecAddr; + Flags = OriginalFlags = SecFlags; + OriginalOffset = SecOff; + } + + void appendHexData(StringRef HexData); + void accept(SectionVisitor &Sec) const override; + void accept(MutableSectionVisitor &Visitor) override; +}; + +class CompressedSection : public SectionBase { + MAKE_SEC_WRITER_FRIEND + + DebugCompressionType CompressionType; + uint64_t DecompressedSize; + uint64_t DecompressedAlign; + SmallVector CompressedData; + +public: + CompressedSection(const SectionBase &Sec, + DebugCompressionType CompressionType); + CompressedSection(ArrayRef CompressedData, uint64_t DecompressedSize, + uint64_t DecompressedAlign); + + uint64_t getDecompressedSize() const { return DecompressedSize; } + uint64_t getDecompressedAlign() const { return DecompressedAlign; } + + void accept(SectionVisitor &Visitor) const override; + void accept(MutableSectionVisitor &Visitor) override; + + static bool classof(const SectionBase *S) { + return (S->OriginalFlags & ELF::SHF_COMPRESSED) || + (StringRef(S->Name).startswith(".zdebug")); + } +}; + +class DecompressedSection : public SectionBase { + MAKE_SEC_WRITER_FRIEND + +public: + explicit DecompressedSection(const CompressedSection &Sec) + : SectionBase(Sec) { + Size = Sec.getDecompressedSize(); + Align = Sec.getDecompressedAlign(); + Flags = OriginalFlags = (Flags & ~ELF::SHF_COMPRESSED); + if (StringRef(Name).startswith(".zdebug")) + Name = "." + Name.substr(2); + } + + void accept(SectionVisitor &Visitor) const override; + void accept(MutableSectionVisitor &Visitor) override; +}; + +// There are two types of string tables that can exist, dynamic and not dynamic. +// In the dynamic case the string table is allocated. Changing a dynamic string +// table would mean altering virtual addresses and thus the memory image. So +// dynamic string tables should not have an interface to modify them or +// reconstruct them. This type lets us reconstruct a string table. To avoid +// this class being used for dynamic string tables (which has happened) the +// classof method checks that the particular instance is not allocated. This +// then agrees with the makeSection method used to construct most sections. +class StringTableSection : public SectionBase { + MAKE_SEC_WRITER_FRIEND + + StringTableBuilder StrTabBuilder; + +public: + StringTableSection() : StrTabBuilder(StringTableBuilder::ELF) { + Type = OriginalType = ELF::SHT_STRTAB; + } + + void addString(StringRef Name); + uint32_t findIndex(StringRef Name) const; + void prepareForLayout(); + void accept(SectionVisitor &Visitor) const override; + void accept(MutableSectionVisitor &Visitor) override; + + static bool classof(const SectionBase *S) { + if (S->OriginalFlags & ELF::SHF_ALLOC) + return false; + return S->OriginalType == ELF::SHT_STRTAB; + } +}; + +// Symbols have a st_shndx field that normally stores an index but occasionally +// stores a different special value. This enum keeps track of what the st_shndx +// field means. Most of the values are just copies of the special SHN_* values. +// SYMBOL_SIMPLE_INDEX means that the st_shndx is just an index of a section. +enum SymbolShndxType { + SYMBOL_SIMPLE_INDEX = 0, + SYMBOL_ABS = ELF::SHN_ABS, + SYMBOL_COMMON = ELF::SHN_COMMON, + SYMBOL_LOPROC = ELF::SHN_LOPROC, + SYMBOL_AMDGPU_LDS = ELF::SHN_AMDGPU_LDS, + SYMBOL_HEXAGON_SCOMMON = ELF::SHN_HEXAGON_SCOMMON, + SYMBOL_HEXAGON_SCOMMON_2 = ELF::SHN_HEXAGON_SCOMMON_2, + SYMBOL_HEXAGON_SCOMMON_4 = ELF::SHN_HEXAGON_SCOMMON_4, + SYMBOL_HEXAGON_SCOMMON_8 = ELF::SHN_HEXAGON_SCOMMON_8, + SYMBOL_HIPROC = ELF::SHN_HIPROC, + SYMBOL_LOOS = ELF::SHN_LOOS, + SYMBOL_HIOS = ELF::SHN_HIOS, + SYMBOL_XINDEX = ELF::SHN_XINDEX, +}; + +struct Symbol { + uint8_t Binding; + SectionBase *DefinedIn = nullptr; + SymbolShndxType ShndxType; + uint32_t Index; + std::string Name; + uint32_t NameIndex; + uint64_t Size; + uint8_t Type; + uint64_t Value; + uint8_t Visibility; + bool Referenced = false; + + uint16_t getShndx() const; + bool isCommon() const; +}; + +class SectionIndexSection : public SectionBase { + MAKE_SEC_WRITER_FRIEND + +private: + std::vector Indexes; + SymbolTableSection *Symbols = nullptr; + +public: + virtual ~SectionIndexSection() {} + void addIndex(uint32_t Index) { + assert(Size > 0); + Indexes.push_back(Index); + } + + void reserve(size_t NumSymbols) { + Indexes.reserve(NumSymbols); + Size = NumSymbols * 4; + } + void setSymTab(SymbolTableSection *SymTab) { Symbols = SymTab; } + void initialize(SectionTableRef SecTable) override; + void finalize() override; + void accept(SectionVisitor &Visitor) const override; + void accept(MutableSectionVisitor &Visitor) override; + + SectionIndexSection() { + Name = ".symtab_shndx"; + Align = 4; + EntrySize = 4; + Type = OriginalType = ELF::SHT_SYMTAB_SHNDX; + } +}; + +class SymbolTableSection : public SectionBase { + MAKE_SEC_WRITER_FRIEND + + void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; } + void assignIndices(); + +protected: + std::vector> Symbols; + StringTableSection *SymbolNames = nullptr; + SectionIndexSection *SectionIndexTable = nullptr; + + using SymPtr = std::unique_ptr; + +public: + SymbolTableSection() { Type = OriginalType = ELF::SHT_SYMTAB; } + + void addSymbol(Twine Name, uint8_t Bind, uint8_t Type, SectionBase *DefinedIn, + uint64_t Value, uint8_t Visibility, uint16_t Shndx, + uint64_t SymbolSize); + void prepareForLayout(); + // An 'empty' symbol table still contains a null symbol. + bool empty() const { return Symbols.size() == 1; } + void setShndxTable(SectionIndexSection *ShndxTable) { + SectionIndexTable = ShndxTable; + } + const SectionIndexSection *getShndxTable() const { return SectionIndexTable; } + void fillShndxTable(); + const SectionBase *getStrTab() const { return SymbolNames; } + const Symbol *getSymbolByIndex(uint32_t Index) const; + Symbol *getSymbolByIndex(uint32_t Index); + void updateSymbols(function_ref Callable); + + Error removeSectionReferences( + bool AllowBrokenLinks, + function_ref ToRemove) override; + void initialize(SectionTableRef SecTable) override; + void finalize() override; + void accept(SectionVisitor &Visitor) const override; + void accept(MutableSectionVisitor &Visitor) override; + Error removeSymbols(function_ref ToRemove) override; + void replaceSectionReferences( + const DenseMap &FromTo) override; + + static bool classof(const SectionBase *S) { + return S->OriginalType == ELF::SHT_SYMTAB; + } +}; + +struct Relocation { + Symbol *RelocSymbol = nullptr; + uint64_t Offset; + uint64_t Addend; + uint32_t Type; +}; + +// All relocation sections denote relocations to apply to another section. +// However, some relocation sections use a dynamic symbol table and others use +// a regular symbol table. Because the types of the two symbol tables differ in +// our system (because they should behave differently) we can't uniformly +// represent all relocations with the same base class if we expose an interface +// that mentions the symbol table type. So we split the two base types into two +// different classes, one which handles the section the relocation is applied to +// and another which handles the symbol table type. The symbol table type is +// taken as a type parameter to the class (see RelocSectionWithSymtabBase). +class RelocationSectionBase : public SectionBase { +protected: + SectionBase *SecToApplyRel = nullptr; + +public: + const SectionBase *getSection() const { return SecToApplyRel; } + void setSection(SectionBase *Sec) { SecToApplyRel = Sec; } + + static bool classof(const SectionBase *S) { + return S->OriginalType == ELF::SHT_REL || S->OriginalType == ELF::SHT_RELA; + } +}; + +// Takes the symbol table type to use as a parameter so that we can deduplicate +// that code between the two symbol table types. +template +class RelocSectionWithSymtabBase : public RelocationSectionBase { + void setSymTab(SymTabType *SymTab) { Symbols = SymTab; } + +protected: + RelocSectionWithSymtabBase() = default; + + SymTabType *Symbols = nullptr; + +public: + void initialize(SectionTableRef SecTable) override; + void finalize() override; +}; + +class RelocationSection + : public RelocSectionWithSymtabBase { + MAKE_SEC_WRITER_FRIEND + + std::vector Relocations; + +public: + void addRelocation(Relocation Rel) { Relocations.push_back(Rel); } + void accept(SectionVisitor &Visitor) const override; + void accept(MutableSectionVisitor &Visitor) override; + Error removeSectionReferences( + bool AllowBrokenLinks, + function_ref ToRemove) override; + Error removeSymbols(function_ref ToRemove) override; + void markSymbols() override; + void replaceSectionReferences( + const DenseMap &FromTo) override; + + static bool classof(const SectionBase *S) { + if (S->OriginalFlags & ELF::SHF_ALLOC) + return false; + return S->OriginalType == ELF::SHT_REL || S->OriginalType == ELF::SHT_RELA; + } +}; + +// TODO: The way stripping and groups interact is complicated +// and still needs to be worked on. + +class GroupSection : public SectionBase { + MAKE_SEC_WRITER_FRIEND + const SymbolTableSection *SymTab = nullptr; + Symbol *Sym = nullptr; + ELF::Elf32_Word FlagWord; + SmallVector GroupMembers; + +public: + // TODO: Contents is present in several classes of the hierarchy. + // This needs to be refactored to avoid duplication. + ArrayRef Contents; + + explicit GroupSection(ArrayRef Data) : Contents(Data) {} + + void setSymTab(const SymbolTableSection *SymTabSec) { SymTab = SymTabSec; } + void setSymbol(Symbol *S) { Sym = S; } + void setFlagWord(ELF::Elf32_Word W) { FlagWord = W; } + void addMember(SectionBase *Sec) { GroupMembers.push_back(Sec); } + + void accept(SectionVisitor &) const override; + void accept(MutableSectionVisitor &Visitor) override; + void finalize() override; + Error removeSectionReferences( + bool AllowBrokenLinks, + function_ref ToRemove) override; + Error removeSymbols(function_ref ToRemove) override; + void markSymbols() override; + void replaceSectionReferences( + const DenseMap &FromTo) override; + void onRemove() override; + + static bool classof(const SectionBase *S) { + return S->OriginalType == ELF::SHT_GROUP; + } +}; + +class DynamicSymbolTableSection : public Section { +public: + explicit DynamicSymbolTableSection(ArrayRef Data) : Section(Data) {} + + static bool classof(const SectionBase *S) { + return S->OriginalType == ELF::SHT_DYNSYM; + } +}; + +class DynamicSection : public Section { +public: + explicit DynamicSection(ArrayRef Data) : Section(Data) {} + + static bool classof(const SectionBase *S) { + return S->OriginalType == ELF::SHT_DYNAMIC; + } +}; + +class DynamicRelocationSection + : public RelocSectionWithSymtabBase { + MAKE_SEC_WRITER_FRIEND + +private: + ArrayRef Contents; + +public: + explicit DynamicRelocationSection(ArrayRef Data) : Contents(Data) {} + + void accept(SectionVisitor &) const override; + void accept(MutableSectionVisitor &Visitor) override; + Error removeSectionReferences( + bool AllowBrokenLinks, + function_ref ToRemove) override; + + static bool classof(const SectionBase *S) { + if (!(S->OriginalFlags & ELF::SHF_ALLOC)) + return false; + return S->OriginalType == ELF::SHT_REL || S->OriginalType == ELF::SHT_RELA; + } +}; + +class GnuDebugLinkSection : public SectionBase { + MAKE_SEC_WRITER_FRIEND + +private: + StringRef FileName; + uint32_t CRC32; + + void init(StringRef File); + +public: + // If we add this section from an external source we can use this ctor. + explicit GnuDebugLinkSection(StringRef File, uint32_t PrecomputedCRC); + void accept(SectionVisitor &Visitor) const override; + void accept(MutableSectionVisitor &Visitor) override; +}; + +class Reader { +public: + virtual ~Reader(); + virtual std::unique_ptr create(bool EnsureSymtab) const = 0; +}; + +using object::Binary; +using object::ELFFile; +using object::ELFObjectFile; +using object::OwningBinary; + +class BasicELFBuilder { +protected: + std::unique_ptr Obj; + + void initFileHeader(); + void initHeaderSegment(); + StringTableSection *addStrTab(); + SymbolTableSection *addSymTab(StringTableSection *StrTab); + void initSections(); + +public: + BasicELFBuilder() : Obj(std::make_unique()) {} +}; + +class BinaryELFBuilder : public BasicELFBuilder { + MemoryBuffer *MemBuf; + uint8_t NewSymbolVisibility; + void addData(SymbolTableSection *SymTab); + +public: + BinaryELFBuilder(MemoryBuffer *MB, uint8_t NewSymbolVisibility) + : BasicELFBuilder(), MemBuf(MB), + NewSymbolVisibility(NewSymbolVisibility) {} + + std::unique_ptr build(); +}; + +class IHexELFBuilder : public BasicELFBuilder { + const std::vector &Records; + + void addDataSections(); + +public: + IHexELFBuilder(const std::vector &Records) + : BasicELFBuilder(), Records(Records) {} + + std::unique_ptr build(); +}; + +template class ELFBuilder { +private: + using Elf_Addr = typename ELFT::Addr; + using Elf_Shdr = typename ELFT::Shdr; + using Elf_Word = typename ELFT::Word; + + const ELFFile &ElfFile; + Object &Obj; + size_t EhdrOffset = 0; + Optional ExtractPartition; + + void setParentSegment(Segment &Child); + void readProgramHeaders(const ELFFile &HeadersFile); + void initGroupSection(GroupSection *GroupSec); + void initSymbolTable(SymbolTableSection *SymTab); + void readSectionHeaders(); + void readSections(bool EnsureSymtab); + void findEhdrOffset(); + SectionBase &makeSection(const Elf_Shdr &Shdr); + +public: + ELFBuilder(const ELFObjectFile &ElfObj, Object &Obj, + Optional ExtractPartition) + : ElfFile(*ElfObj.getELFFile()), Obj(Obj), + ExtractPartition(ExtractPartition) {} + + void build(bool EnsureSymtab); +}; + +class BinaryReader : public Reader { + MemoryBuffer *MemBuf; + uint8_t NewSymbolVisibility; + +public: + BinaryReader(MemoryBuffer *MB, const uint8_t NewSymbolVisibility) + : MemBuf(MB), NewSymbolVisibility(NewSymbolVisibility) {} + std::unique_ptr create(bool EnsureSymtab) const override; +}; + +class IHexReader : public Reader { + MemoryBuffer *MemBuf; + + Expected> parse() const; + Error parseError(size_t LineNo, Error E) const { + return LineNo == -1U + ? createFileError(MemBuf->getBufferIdentifier(), std::move(E)) + : createFileError(MemBuf->getBufferIdentifier(), LineNo, + std::move(E)); + } + template + Error parseError(size_t LineNo, char const *Fmt, const Ts &... Vals) const { + Error E = createStringError(errc::invalid_argument, Fmt, Vals...); + return parseError(LineNo, std::move(E)); + } + +public: + IHexReader(MemoryBuffer *MB) : MemBuf(MB) {} + + std::unique_ptr create(bool EnsureSymtab) const override; +}; + +class ELFReader : public Reader { + Binary *Bin; + Optional ExtractPartition; + +public: + std::unique_ptr create(bool EnsureSymtab) const override; + explicit ELFReader(Binary *B, Optional ExtractPartition) + : Bin(B), ExtractPartition(ExtractPartition) {} +}; + +class Object { +private: + using SecPtr = std::unique_ptr; + using SegPtr = std::unique_ptr; + + std::vector Sections; + std::vector Segments; + std::vector RemovedSections; + + static bool sectionIsAlloc(const SectionBase &Sec) { + return Sec.Flags & ELF::SHF_ALLOC; + }; + +public: + template + using Range = iterator_range< + pointee_iterator>::iterator>>; + + template + using ConstRange = iterator_range>::const_iterator>>; + + // It is often the case that the ELF header and the program header table are + // not present in any segment. This could be a problem during file layout, + // because other segments may get assigned an offset where either of the + // two should reside, which will effectively corrupt the resulting binary. + // Other than that we use these segments to track program header offsets + // when they may not follow the ELF header. + Segment ElfHdrSegment; + Segment ProgramHdrSegment; + + uint8_t OSABI; + uint8_t ABIVersion; + uint64_t Entry; + uint64_t SHOff; + uint32_t Type; + uint32_t Machine; + uint32_t Version; + uint32_t Flags; + + bool HadShdrs = true; + bool MustBeRelocatable = false; + StringTableSection *SectionNames = nullptr; + SymbolTableSection *SymbolTable = nullptr; + SectionIndexSection *SectionIndexTable = nullptr; + + void sortSections(); + SectionTableRef sections() { return SectionTableRef(Sections); } + ConstRange sections() const { + return make_pointee_range(Sections); + } + iterator_range< + filter_iterator::const_iterator>, + decltype(§ionIsAlloc)>> + allocSections() const { + return make_filter_range(make_pointee_range(Sections), sectionIsAlloc); + } + + SectionBase *findSection(StringRef Name) { + auto SecIt = + find_if(Sections, [&](const SecPtr &Sec) { return Sec->Name == Name; }); + return SecIt == Sections.end() ? nullptr : SecIt->get(); + } + SectionTableRef removedSections() { return SectionTableRef(RemovedSections); } + + Range segments() { return make_pointee_range(Segments); } + ConstRange segments() const { return make_pointee_range(Segments); } + + Error removeSections(bool AllowBrokenLinks, + std::function ToRemove); + Error removeSymbols(function_ref ToRemove); + template T &addSection(Ts &&... Args) { + auto Sec = std::make_unique(std::forward(Args)...); + auto Ptr = Sec.get(); + MustBeRelocatable |= isa(*Ptr); + Sections.emplace_back(std::move(Sec)); + Ptr->Index = Sections.size(); + return *Ptr; + } + void addNewSymbolTable(); + Segment &addSegment(ArrayRef Data) { + Segments.emplace_back(std::make_unique(Data)); + return *Segments.back(); + } + bool isRelocatable() const { + return (Type != ELF::ET_DYN && Type != ELF::ET_EXEC) || MustBeRelocatable; + } +}; + +} // end namespace elf +} // end namespace objectcopy +} // end namespace llvm + +#endif // LLVM_OBJECTCOPY_ELF_OBJECT_H diff --git a/llvm/lib/CMakeLists.txt b/llvm/lib/CMakeLists.txt --- a/llvm/lib/CMakeLists.txt +++ b/llvm/lib/CMakeLists.txt @@ -19,6 +19,7 @@ add_subdirectory(MC) add_subdirectory(MCA) add_subdirectory(Object) +add_subdirectory(ObjectCopy) add_subdirectory(ObjectYAML) add_subdirectory(Option) add_subdirectory(Remarks) diff --git a/llvm/lib/DWARFLinker/DWARFLinker.cpp b/llvm/lib/DWARFLinker/DWARFLinker.cpp --- a/llvm/lib/DWARFLinker/DWARFLinker.cpp +++ b/llvm/lib/DWARFLinker/DWARFLinker.cpp @@ -309,11 +309,7 @@ // Only prune forward declarations inside a DW_TAG_module for which a // definition exists elsewhere. - if (ModulesEndOffset == 0) - Info.Prune &= Info.Ctxt && Info.Ctxt->getCanonicalDIEOffset(); - else - Info.Prune &= Info.Ctxt && Info.Ctxt->getCanonicalDIEOffset() > 0 && - Info.Ctxt->getCanonicalDIEOffset() <= ModulesEndOffset; + Info.Prune &= Info.Ctxt && Info.Ctxt->hasCanonicalDie(); return Info.Prune; } @@ -347,27 +343,6 @@ DIEAlloc.Reset(); } -/// Get the starting and ending (exclusive) offset for the -/// attribute with index \p Idx descibed by \p Abbrev. \p Offset is -/// supposed to point to the position of the first attribute described -/// by \p Abbrev. -/// \return [StartOffset, EndOffset) as a pair. -static std::pair -getAttributeOffsets(const DWARFAbbreviationDeclaration *Abbrev, unsigned Idx, - uint64_t Offset, const DWARFUnit &Unit) { - DataExtractor Data = Unit.getDebugInfoExtractor(); - - for (unsigned I = 0; I < Idx; ++I) - DWARFFormValue::skipValue(Abbrev->getFormByIndex(I), Data, &Offset, - Unit.getFormParams()); - - uint64_t End = Offset; - DWARFFormValue::skipValue(Abbrev->getFormByIndex(Idx), Data, &End, - Unit.getFormParams()); - - return std::make_pair(Offset, End); -} - /// Check if a variable describing DIE should be kept. /// \returns updated TraversalFlags. unsigned DWARFLinker::shouldKeepVariableDIE(AddressesMap &RelocMgr, @@ -384,24 +359,14 @@ return Flags | TF_Keep; } - Optional LocationIdx = - Abbrev->findAttributeIndex(dwarf::DW_AT_location); - if (!LocationIdx) + if (!Abbrev->findAttributeIndex(dwarf::DW_AT_location)) return Flags; - uint64_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode()); - const DWARFUnit &OrigUnit = Unit.getOrigUnit(); - uint64_t LocationOffset, LocationEndOffset; - std::tie(LocationOffset, LocationEndOffset) = - getAttributeOffsets(Abbrev, *LocationIdx, Offset, OrigUnit); - - // See if there is a relocation to a valid debug map entry inside - // this variable's location. The order is important here. We want to - // always check if the variable has a valid relocation, so that the + // See if there is a live variable's location. The order is important here. + // We want to always check if the variable has a live location, so that the // DIEInfo is filled. However, we don't want a static variable in a // function to force us to keep the enclosing function. - if (!RelocMgr.hasValidRelocationAt(LocationOffset, LocationEndOffset, - MyInfo) || + if (!RelocMgr.isLiveLocationAttribute(DIE, dwarf::DW_AT_location, MyInfo) || (Flags & TF_InFunctionScope)) return Flags; @@ -422,26 +387,18 @@ AddressesMap &RelocMgr, RangesTy &Ranges, const DWARFDie &DIE, const DwarfFile &File, CompileUnit &Unit, CompileUnit::DIEInfo &MyInfo, unsigned Flags) { - const auto *Abbrev = DIE.getAbbreviationDeclarationPtr(); - Flags |= TF_InFunctionScope; - Optional LowPcIdx = Abbrev->findAttributeIndex(dwarf::DW_AT_low_pc); - if (!LowPcIdx) - return Flags; - - uint64_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode()); - DWARFUnit &OrigUnit = Unit.getOrigUnit(); - uint64_t LowPcOffset, LowPcEndOffset; - std::tie(LowPcOffset, LowPcEndOffset) = - getAttributeOffsets(Abbrev, *LowPcIdx, Offset, OrigUnit); - auto LowPc = dwarf::toAddress(DIE.find(dwarf::DW_AT_low_pc)); + if (!LowPc) + return Flags; assert(LowPc.hasValue() && "low_pc attribute is not an address."); - if (!LowPc || - !RelocMgr.hasValidRelocationAt(LowPcOffset, LowPcEndOffset, MyInfo)) + + if (!RelocMgr.isLiveAddressAttribute(DIE, dwarf::DW_AT_low_pc, MyInfo)) return Flags; + DWARFUnit &OrigUnit = Unit.getOrigUnit(); + if (Options.Verbose) { outs() << "Keeping subprogram DIE:"; DIDumpOptions DumpOpts; @@ -476,6 +433,7 @@ // Replace the debug map range with a more accurate one. Ranges[*LowPc] = ObjFileAddressRange(*HighPc, MyInfo.AddrAdjust); Unit.addFunctionRange(*LowPc, *HighPc, MyInfo.AddrAdjust); + return Flags; } @@ -575,7 +533,7 @@ // Add children in reverse order to the worklist to effectively process them // in order. - for (auto Child : reverse(Die.children())) { + for (const auto &Child : reverse(Die.children())) { // Add a worklist item before every child to calculate incompleteness right // after the current child is processed. unsigned Idx = CU.getOrigUnit().getDIEIndex(Child); @@ -616,7 +574,7 @@ resolveDIEReference(File, Units, Val, Die, ReferencedCU)) { uint32_t RefIdx = ReferencedCU->getOrigUnit().getDIEIndex(RefDie); CompileUnit::DIEInfo &Info = ReferencedCU->getInfo(RefIdx); - bool IsModuleRef = Info.Ctxt && Info.Ctxt->getCanonicalDIEOffset() && + bool IsModuleRef = Info.Ctxt && Info.Ctxt->hasCanonicalDie() && Info.Ctxt->isDefinedInClangModule(); // If the referenced DIE has a DeclContext that has already been // emitted, then do not keep the one in this CU. We'll link to @@ -631,13 +589,14 @@ if (AttrSpec.Form != dwarf::DW_FORM_ref_addr && (UseOdr || IsModuleRef) && Info.Ctxt && Info.Ctxt != ReferencedCU->getInfo(Info.ParentIdx).Ctxt && - Info.Ctxt->getCanonicalDIEOffset() && isODRAttribute(AttrSpec.Attr)) + Info.Ctxt->hasCanonicalDie() && isODRAttribute(AttrSpec.Attr)) continue; // Keep a module forward declaration if there is no definition. if (!(isODRAttribute(AttrSpec.Attr) && Info.Ctxt && - Info.Ctxt->getCanonicalDIEOffset())) + Info.Ctxt->hasCanonicalDie())) Info.Prune = false; + ReferencedDIEs.emplace_back(RefDie, *ReferencedCU); } } @@ -729,6 +688,9 @@ lookForParentDIEsToKeep(Current.AncestorIdx, Current.CU, Current.Flags, Worklist); continue; + case WorklistItemType::MarkODRCanonicalDie: + Current.CU.markODRCanonicalDie(Current.Die, *Current.OtherInfo); + continue; case WorklistItemType::LookForDIEsToKeep: break; } @@ -739,6 +701,10 @@ if (MyInfo.Prune) continue; + if (Current.CU.isODRCanonicalCandidate(Current.Die)) + Worklist.emplace_back(Current.Die, Current.CU, + WorklistItemType::MarkODRCanonicalDie, &MyInfo); + // If the Keep flag is set, we are marking a required DIE's dependencies. // If our target is already marked as kept, we're all set. bool AlreadyKept = MyInfo.Keep; @@ -861,7 +827,7 @@ // at it. if (isODRAttribute(AttrSpec.Attr)) { Ctxt = RefInfo.Ctxt; - if (Ctxt && Ctxt->getCanonicalDIEOffset()) { + if (Ctxt && Ctxt->hasCanonicalDie() && Ctxt->getCanonicalDIEOffset()) { DIEInteger Attr(Ctxt->getCanonicalDIEOffset()); Die.addValue(DIEAlloc, dwarf::Attribute(AttrSpec.Attr), dwarf::DW_FORM_ref_addr, Attr); @@ -1298,10 +1264,9 @@ assert(Die->getTag() == InputDIE.getTag()); Die->setOffset(OutOffset); - if ((Unit.hasODR() || Unit.isClangModule()) && !Info.Incomplete && - Die->getTag() != dwarf::DW_TAG_namespace && Info.Ctxt && - Info.Ctxt != Unit.getInfo(Info.ParentIdx).Ctxt && - !Info.Ctxt->getCanonicalDIEOffset()) { + + if (Info.Ctxt && Info.IsODRCanonical) { + assert(Info.Ctxt->hasCanonicalDie()); // We are about to emit a DIE that is the root of its own valid // DeclContext tree. Make the current offset the canonical offset // for this context. @@ -1739,6 +1704,9 @@ case AccelTableKind::Dwarf: emitDwarfAcceleratorEntriesForUnit(Unit); break; + case AccelTableKind::None: + // nothing to do + break; case AccelTableKind::Default: llvm_unreachable("The default must be updated to a concrete value."); break; @@ -2401,6 +2369,7 @@ !registerModuleReference(CUDie, *CU, Context.File, OffsetsStringPool, UniquingStringPool, ODRContexts, ModulesEndOffset, UnitID, Quiet)) { + Context.CompileUnits.push_back(std::make_unique( *CU, UnitID++, !Options.NoODR && !Options.Update, "")); } @@ -2491,6 +2460,8 @@ case AccelTableKind::Dwarf: TheDwarfEmitter->emitDebugNames(DebugNames); break; + case AccelTableKind::None: + break; case AccelTableKind::Default: llvm_unreachable("Default should have already been resolved."); break; diff --git a/llvm/lib/DWARFLinker/DWARFLinkerCompileUnit.cpp b/llvm/lib/DWARFLinker/DWARFLinkerCompileUnit.cpp --- a/llvm/lib/DWARFLinker/DWARFLinkerCompileUnit.cpp +++ b/llvm/lib/DWARFLinker/DWARFLinkerCompileUnit.cpp @@ -36,7 +36,38 @@ } return SysRoot; } - + +bool CompileUnit::inModuleScope(const DWARFDie &Die) { + unsigned Idx = getOrigUnit().getDIEIndex(Die); + + while (Idx) { + DWARFDie Die = getOrigUnit().getDIEAtIndex(Idx); + if (Die.getTag() == dwarf::DW_TAG_module) { + const char *Name = Die.getShortName(); + if (Name && Name == ClangModuleName) + return true; + } + Idx = getInfo(Idx).ParentIdx; + } + return false; +} + +bool CompileUnit::isODRCanonicalCandidate(const DWARFDie &Die) { + return hasODR() || (isClangModule() && inModuleScope(Die)); +} + +void CompileUnit::markODRCanonicalDie(const DWARFDie &Die, + CompileUnit::DIEInfo &Info) { + assert(isODRCanonicalCandidate(Die)); + + if (Info.Keep && !Info.Incomplete && Info.Ctxt && + !Info.Ctxt->hasCanonicalDie() && + Info.Ctxt != getInfo(Info.ParentIdx).Ctxt) { + Info.Ctxt->setHasCanonicalDie(); + Info.IsODRCanonical = true; + } +} + void CompileUnit::markEverythingAsKept() { unsigned Idx = 0; @@ -47,6 +78,9 @@ I.Keep = !I.Prune; auto DIE = OrigUnit.getDIEAtIndex(Idx++); + if (isODRCanonicalCandidate(DIE)) + markODRCanonicalDie(DIE, I); + // Try to guess which DIEs must go to the accelerator tables. We do that // just for variables, because functions will be handled depending on // whether they carry a DW_AT_low_pc attribute or not. @@ -92,7 +126,8 @@ PatchLocation Attr; DeclContext *Ctxt; std::tie(RefDie, RefUnit, Ctxt, Attr) = Ref; - if (Ctxt && Ctxt->getCanonicalDIEOffset()) + + if (Ctxt && Ctxt->hasCanonicalDie() && Ctxt->getCanonicalDIEOffset()) Attr.set(Ctxt->getCanonicalDIEOffset()); else Attr.set(RefDie->getOffset() + RefUnit->getStartOffset()); diff --git a/llvm/lib/DWARFLinker/DWARFStreamer.cpp b/llvm/lib/DWARFLinker/DWARFStreamer.cpp --- a/llvm/lib/DWARFLinker/DWARFStreamer.cpp +++ b/llvm/lib/DWARFLinker/DWARFStreamer.cpp @@ -49,6 +49,7 @@ MAI.reset(TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions)); if (!MAI) return error("no asm info for target " + TripleName, Context), false; + MAI->setDwarfUseRelocationsAcrossSections(false); MOFI.reset(new MCObjectFileInfo); MC.reset(new MCContext(MAI.get(), MRI.get(), MOFI.get())); @@ -102,6 +103,8 @@ Asm.reset(TheTarget->createAsmPrinter(*TM, std::unique_ptr(MS))); if (!Asm) return error("no asm printer for target " + TripleName, Context), false; + const_cast(Asm->MAI)->setDwarfUseRelocationsAcrossSections( + false); RangesSectionSize = 0; LocSectionSize = 0; diff --git a/llvm/lib/LLVMBuild.txt b/llvm/lib/LLVMBuild.txt --- a/llvm/lib/LLVMBuild.txt +++ b/llvm/lib/LLVMBuild.txt @@ -37,6 +37,7 @@ MC MCA Object + ObjectCopy BinaryFormat ObjectYAML Option diff --git a/llvm/lib/ObjectCopy/Buffer.cpp b/llvm/lib/ObjectCopy/Buffer.cpp new file mode 100644 --- /dev/null +++ b/llvm/lib/ObjectCopy/Buffer.cpp @@ -0,0 +1,79 @@ +//===- Buffer.cpp ---------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "llvm/ObjectCopy/Buffer.h" +#include "llvm/Support/FileOutputBuffer.h" +#include "llvm/Support/FileSystem.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/Process.h" +#include + +namespace llvm { +namespace objectcopy { + +Buffer::~Buffer() {} + +static Error createEmptyFile(StringRef FileName) { + // Create an empty tempfile and atomically swap it in place with the desired + // output file. + Expected Temp = + sys::fs::TempFile::create(FileName + ".temp-empty-%%%%%%%"); + return Temp ? Temp->keep(FileName) : Temp.takeError(); +} + +Error FileBuffer::allocate(size_t Size) { + // When a 0-sized file is requested, skip allocation but defer file + // creation/truncation until commit() to avoid side effects if something + // happens between allocate() and commit(). + if (Size == 0) { + EmptyFile = true; + return Error::success(); + } + + Expected> BufferOrErr = + FileOutputBuffer::create(getName(), Size, FileOutputBuffer::F_executable); + // FileOutputBuffer::create() returns an Error that is just a wrapper around + // std::error_code. Wrap it in FileError to include the actual filename. + if (!BufferOrErr) + return createFileError(getName(), BufferOrErr.takeError()); + Buf = std::move(*BufferOrErr); + return Error::success(); +} + +Error FileBuffer::commit() { + if (EmptyFile) + return createEmptyFile(getName()); + + assert(Buf && "allocate() not called before commit()!"); + Error Err = Buf->commit(); + // FileOutputBuffer::commit() returns an Error that is just a wrapper around + // std::error_code. Wrap it in FileError to include the actual filename. + return Err ? createFileError(getName(), std::move(Err)) : std::move(Err); +} + +uint8_t *FileBuffer::getBufferStart() { + return reinterpret_cast(Buf->getBufferStart()); +} + +Error MemBuffer::allocate(size_t Size) { + Buf = WritableMemoryBuffer::getNewMemBuffer(Size, getName()); + return Error::success(); +} + +Error MemBuffer::commit() { return Error::success(); } + +uint8_t *MemBuffer::getBufferStart() { + return reinterpret_cast(Buf->getBufferStart()); +} + +std::unique_ptr MemBuffer::releaseMemoryBuffer() { + return std::move(Buf); +} + +} // end namespace objectcopy +} // end namespace llvm diff --git a/llvm/lib/ObjectCopy/CMakeLists.txt b/llvm/lib/ObjectCopy/CMakeLists.txt new file mode 100644 --- /dev/null +++ b/llvm/lib/ObjectCopy/CMakeLists.txt @@ -0,0 +1,13 @@ +add_llvm_component_library(LLVMObjectCopy + Buffer.cpp + CopyConfig.cpp + ELF/ELFConfig.cpp + ELF/ELFObjcopy.cpp + ELF/Object.cpp + + ADDITIONAL_HEADER_DIRS + ${LLVM_MAIN_INCLUDE_DIR}/llvm/ObjectCopy + + DEPENDS + intrinsics_gen + ) diff --git a/llvm/lib/ObjectCopy/CopyConfig.cpp b/llvm/lib/ObjectCopy/CopyConfig.cpp new file mode 100644 --- /dev/null +++ b/llvm/lib/ObjectCopy/CopyConfig.cpp @@ -0,0 +1,152 @@ +//===- CopyConfig.cpp -----------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "llvm/ObjectCopy/CopyConfig.h" + +#include "llvm/ADT/Optional.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/ADT/StringSet.h" +#include "llvm/Option/Arg.h" +#include "llvm/Option/ArgList.h" +#include "llvm/Support/CRC.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/Compression.h" +#include "llvm/Support/Errc.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/StringSaver.h" +#include "llvm/Support/WithColor.h" +#include + +namespace llvm { +namespace objectcopy { + +struct TargetInfo { + FileFormat Format; + MachineInfo Machine; +}; + +// FIXME: consolidate with the bfd parsing used by lld. +static const StringMap TargetMap{ + // Name, {EMachine, 64bit, LittleEndian} + // x86 + {"elf32-i386", {ELF::EM_386, false, true}}, + {"elf32-x86-64", {ELF::EM_X86_64, false, true}}, + {"elf64-x86-64", {ELF::EM_X86_64, true, true}}, + // Intel MCU + {"elf32-iamcu", {ELF::EM_IAMCU, false, true}}, + // ARM + {"elf32-littlearm", {ELF::EM_ARM, false, true}}, + // ARM AArch64 + {"elf64-aarch64", {ELF::EM_AARCH64, true, true}}, + {"elf64-littleaarch64", {ELF::EM_AARCH64, true, true}}, + // RISC-V + {"elf32-littleriscv", {ELF::EM_RISCV, false, true}}, + {"elf64-littleriscv", {ELF::EM_RISCV, true, true}}, + // PowerPC + {"elf32-powerpc", {ELF::EM_PPC, false, false}}, + {"elf32-powerpcle", {ELF::EM_PPC, false, true}}, + {"elf64-powerpc", {ELF::EM_PPC64, true, false}}, + {"elf64-powerpcle", {ELF::EM_PPC64, true, true}}, + // MIPS + {"elf32-bigmips", {ELF::EM_MIPS, false, false}}, + {"elf32-ntradbigmips", {ELF::EM_MIPS, false, false}}, + {"elf32-ntradlittlemips", {ELF::EM_MIPS, false, true}}, + {"elf32-tradbigmips", {ELF::EM_MIPS, false, false}}, + {"elf32-tradlittlemips", {ELF::EM_MIPS, false, true}}, + {"elf64-tradbigmips", {ELF::EM_MIPS, true, false}}, + {"elf64-tradlittlemips", {ELF::EM_MIPS, true, true}}, + // SPARC + {"elf32-sparc", {ELF::EM_SPARC, false, false}}, + {"elf32-sparcel", {ELF::EM_SPARC, false, true}}, + {"elf32-hexagon", {ELF::EM_HEXAGON, false, true}}, +}; + +Expected +NameOrPattern::create(StringRef Pattern, MatchStyle MS, + llvm::function_ref ErrorCallback) { + switch (MS) { + case MatchStyle::Literal: + return NameOrPattern(Pattern); + case MatchStyle::Wildcard: { + SmallVector Data; + bool IsPositiveMatch = true; + if (Pattern[0] == '!') { + IsPositiveMatch = false; + Pattern = Pattern.drop_front(); + } + Expected GlobOrErr = GlobPattern::create(Pattern); + + // If we couldn't create it as a glob, report the error, but try again with + // a literal if the error reporting is non-fatal. + if (!GlobOrErr) { + if (Error E = ErrorCallback(GlobOrErr.takeError())) + return std::move(E); + return create(Pattern, MatchStyle::Literal, ErrorCallback); + } + + return NameOrPattern(std::make_shared(*GlobOrErr), + IsPositiveMatch); + } + case MatchStyle::Regex: { + SmallVector Data; + return NameOrPattern(std::make_shared( + ("^" + Pattern.ltrim('^').rtrim('$') + "$").toStringRef(Data))); + } + } + llvm_unreachable("Unhandled llvm.objcopy.MatchStyle enum"); +} + +template static ErrorOr getAsInteger(StringRef Val) { + T Result; + if (Val.getAsInteger(0, Result)) + return errc::invalid_argument; + return Result; +} + +// The name this program was invoked as. +StringRef ToolName; + +LLVM_ATTRIBUTE_NORETURN void error(Twine Message) { + WithColor::error(errs(), ToolName) << Message << "\n"; + exit(1); +} + +LLVM_ATTRIBUTE_NORETURN void error(Error E) { + assert(E); + std::string Buf; + raw_string_ostream OS(Buf); + logAllUnhandledErrors(std::move(E), OS); + OS.flush(); + WithColor::error(errs(), ToolName) << Buf; + exit(1); +} + +LLVM_ATTRIBUTE_NORETURN void reportError(StringRef File, std::error_code EC) { + assert(EC); + error(createFileError(File, EC)); +} + +LLVM_ATTRIBUTE_NORETURN void reportError(StringRef File, Error E) { + assert(E); + std::string Buf; + raw_string_ostream OS(Buf); + logAllUnhandledErrors(std::move(E), OS); + OS.flush(); + WithColor::error(errs(), ToolName) << "'" << File << "': " << Buf; + exit(1); +} + +ErrorSuccess reportWarning(Error E) { + assert(E); + WithColor::warning(errs(), ToolName) << toString(std::move(E)) << '\n'; + return Error::success(); +} + +} // namespace objectcopy +} // namespace llvm diff --git a/llvm/lib/ObjectCopy/ELF/ELFConfig.cpp b/llvm/lib/ObjectCopy/ELF/ELFConfig.cpp new file mode 100644 --- /dev/null +++ b/llvm/lib/ObjectCopy/ELF/ELFConfig.cpp @@ -0,0 +1,133 @@ +//===- ELFConfig.cpp ------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "llvm/ADT/Optional.h" +#include "llvm/ADT/StringSwitch.h" +#include "llvm/ObjectCopy/CopyConfig.h" +#include "llvm/Support/Errc.h" +#include "llvm/Support/Error.h" + +namespace llvm { +namespace objectcopy { +namespace elf { + +static Expected parseNewSymbolInfo(StringRef FlagValue, + uint8_t DefaultVisibility) { + // Parse value given with --add-symbol option and create the + // new symbol if possible. The value format for --add-symbol is: + // + // =[
:][,] + // + // where: + // - symbol name, can be empty string + //
- optional section name. If not given ABS symbol is created + // - symbol value, can be decimal or hexadecimal number prefixed + // with 0x. + // - optional flags affecting symbol type, binding or visibility: + // The following are currently supported: + // + // global, local, weak, default, hidden, file, section, object, + // indirect-function. + // + // The following flags are ignored and provided for GNU + // compatibility only: + // + // warning, debug, constructor, indirect, synthetic, + // unique-object, before=. + NewSymbolInfo SI; + StringRef Value; + std::tie(SI.SymbolName, Value) = FlagValue.split('='); + if (Value.empty()) + return createStringError( + errc::invalid_argument, + "bad format for --add-symbol, missing '=' after '%s'", + SI.SymbolName.str().c_str()); + + if (Value.contains(':')) { + std::tie(SI.SectionName, Value) = Value.split(':'); + if (SI.SectionName.empty() || Value.empty()) + return createStringError( + errc::invalid_argument, + "bad format for --add-symbol, missing section name or symbol value"); + } + + SmallVector Flags; + Value.split(Flags, ','); + if (Flags[0].getAsInteger(0, SI.Value)) + return createStringError(errc::invalid_argument, "bad symbol value: '%s'", + Flags[0].str().c_str()); + + SI.Visibility = DefaultVisibility; + + using Functor = std::function; + SmallVector UnsupportedFlags; + for (size_t I = 1, NumFlags = Flags.size(); I < NumFlags; ++I) + static_cast( + StringSwitch(Flags[I]) + .CaseLower("global", [&SI] { SI.Bind = ELF::STB_GLOBAL; }) + .CaseLower("local", [&SI] { SI.Bind = ELF::STB_LOCAL; }) + .CaseLower("weak", [&SI] { SI.Bind = ELF::STB_WEAK; }) + .CaseLower("default", [&SI] { SI.Visibility = ELF::STV_DEFAULT; }) + .CaseLower("hidden", [&SI] { SI.Visibility = ELF::STV_HIDDEN; }) + .CaseLower("protected", + [&SI] { SI.Visibility = ELF::STV_PROTECTED; }) + .CaseLower("file", [&SI] { SI.Type = ELF::STT_FILE; }) + .CaseLower("section", [&SI] { SI.Type = ELF::STT_SECTION; }) + .CaseLower("object", [&SI] { SI.Type = ELF::STT_OBJECT; }) + .CaseLower("function", [&SI] { SI.Type = ELF::STT_FUNC; }) + .CaseLower("indirect-function", + [&SI] { SI.Type = ELF::STT_GNU_IFUNC; }) + .CaseLower("debug", [] {}) + .CaseLower("constructor", [] {}) + .CaseLower("warning", [] {}) + .CaseLower("indirect", [] {}) + .CaseLower("synthetic", [] {}) + .CaseLower("unique-object", [] {}) + .StartsWithLower("before", [] {}) + .Default([&] { UnsupportedFlags.push_back(Flags[I]); }))(); + if (!UnsupportedFlags.empty()) + return createStringError(errc::invalid_argument, + "unsupported flag%s for --add-symbol: '%s'", + UnsupportedFlags.size() > 1 ? "s" : "", + join(UnsupportedFlags, "', '").c_str()); + return SI; +} + +Expected parseConfig(const CopyConfig &Config) { + ELFCopyConfig ELFConfig; + if (Config.NewSymbolVisibility) { + const uint8_t Invalid = 0xff; + ELFConfig.NewSymbolVisibility = + StringSwitch(*Config.NewSymbolVisibility) + .Case("default", ELF::STV_DEFAULT) + .Case("hidden", ELF::STV_HIDDEN) + .Case("internal", ELF::STV_INTERNAL) + .Case("protected", ELF::STV_PROTECTED) + .Default(Invalid); + + if (ELFConfig.NewSymbolVisibility == Invalid) + return createStringError(errc::invalid_argument, + "'%s' is not a valid symbol visibility", + Config.NewSymbolVisibility->str().c_str()); + } + + for (StringRef Arg : Config.SymbolsToAdd) { + Expected NSI = parseNewSymbolInfo( + Arg, + ELFConfig.NewSymbolVisibility.getValueOr((uint8_t)ELF::STV_DEFAULT)); + if (!NSI) + return NSI.takeError(); + ELFConfig.SymbolsToAdd.push_back(*NSI); + } + + return ELFConfig; +} + +} // end namespace elf +} // end namespace objectcopy +} // end namespace llvm diff --git a/llvm/lib/ObjectCopy/ELF/ELFObjcopy.cpp b/llvm/lib/ObjectCopy/ELF/ELFObjcopy.cpp new file mode 100644 --- /dev/null +++ b/llvm/lib/ObjectCopy/ELF/ELFObjcopy.cpp @@ -0,0 +1,861 @@ +//===- ELFObjcopy.cpp -----------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "llvm/ObjectCopy/ELF/ELFObjcopy.h" +#include "llvm/ADT/BitmaskEnum.h" +#include "llvm/ADT/DenseSet.h" +#include "llvm/ADT/Optional.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/ADT/Twine.h" +#include "llvm/BinaryFormat/ELF.h" +#include "llvm/MC/MCTargetOptions.h" +#include "llvm/Object/Binary.h" +#include "llvm/Object/ELFObjectFile.h" +#include "llvm/Object/ELFTypes.h" +#include "llvm/Object/Error.h" +#include "llvm/ObjectCopy/Buffer.h" +#include "llvm/ObjectCopy/CopyConfig.h" +#include "llvm/ObjectCopy/ELF/Object.h" +#include "llvm/Option/Option.h" +#include "llvm/Support/Casting.h" +#include "llvm/Support/Compression.h" +#include "llvm/Support/Errc.h" +#include "llvm/Support/Error.h" +#include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/ErrorOr.h" +#include "llvm/Support/FileSystem.h" +#include "llvm/Support/Memory.h" +#include "llvm/Support/Path.h" +#include "llvm/Support/raw_ostream.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace llvm { +namespace objectcopy { +namespace elf { + +using namespace object; +using namespace ELF; +using SectionPred = std::function; + +static bool isDebugSection(const SectionBase &Sec) { + return StringRef(Sec.Name).startswith(".debug") || + StringRef(Sec.Name).startswith(".zdebug") || Sec.Name == ".gdb_index"; +} + +static bool isDWOSection(const SectionBase &Sec) { + return StringRef(Sec.Name).endswith(".dwo"); +} + +static bool onlyKeepDWOPred(const Object &Obj, const SectionBase &Sec) { + // We can't remove the section header string table. + if (&Sec == Obj.SectionNames) + return false; + // Short of keeping the string table we want to keep everything that is a DWO + // section and remove everything else. + return !isDWOSection(Sec); +} + +uint64_t getNewShfFlags(SectionFlag AllFlags) { + uint64_t NewFlags = 0; + if (AllFlags & SectionFlag::SecAlloc) + NewFlags |= ELF::SHF_ALLOC; + if (!(AllFlags & SectionFlag::SecReadonly)) + NewFlags |= ELF::SHF_WRITE; + if (AllFlags & SectionFlag::SecCode) + NewFlags |= ELF::SHF_EXECINSTR; + if (AllFlags & SectionFlag::SecMerge) + NewFlags |= ELF::SHF_MERGE; + if (AllFlags & SectionFlag::SecStrings) + NewFlags |= ELF::SHF_STRINGS; + if (AllFlags & SectionFlag::SecExclude) + NewFlags |= ELF::SHF_EXCLUDE; + return NewFlags; +} + +static uint64_t getSectionFlagsPreserveMask(uint64_t OldFlags, + uint64_t NewFlags) { + // Preserve some flags which should not be dropped when setting flags. + // Also, preserve anything OS/processor dependant. + const uint64_t PreserveMask = + (ELF::SHF_COMPRESSED | ELF::SHF_GROUP | ELF::SHF_LINK_ORDER | + ELF::SHF_MASKOS | ELF::SHF_MASKPROC | ELF::SHF_TLS | + ELF::SHF_INFO_LINK) & + ~ELF::SHF_EXCLUDE; + return (OldFlags & PreserveMask) | (NewFlags & ~PreserveMask); +} + +static void setSectionFlagsAndType(SectionBase &Sec, SectionFlag Flags) { + Sec.Flags = getSectionFlagsPreserveMask(Sec.Flags, getNewShfFlags(Flags)); + + // In GNU objcopy, certain flags promote SHT_NOBITS to SHT_PROGBITS. This rule + // may promote more non-ALLOC sections than GNU objcopy, but it is fine as + // non-ALLOC SHT_NOBITS sections do not make much sense. + if (Sec.Type == SHT_NOBITS && + (!(Sec.Flags & ELF::SHF_ALLOC) || + Flags & (SectionFlag::SecContents | SectionFlag::SecLoad))) + Sec.Type = SHT_PROGBITS; +} + +static ElfType getOutputElfType(const Binary &Bin) { + // Infer output ELF type from the input ELF object + if (isa>(Bin)) + return ELFT_ELF32LE; + if (isa>(Bin)) + return ELFT_ELF64LE; + if (isa>(Bin)) + return ELFT_ELF32BE; + if (isa>(Bin)) + return ELFT_ELF64BE; + llvm_unreachable("Invalid ELFType"); +} + +static ElfType getOutputElfType(const MachineInfo &MI) { + // Infer output ELF type from the binary arch specified + if (MI.Is64Bit) + return MI.IsLittleEndian ? ELFT_ELF64LE : ELFT_ELF64BE; + else + return MI.IsLittleEndian ? ELFT_ELF32LE : ELFT_ELF32BE; +} + +static std::unique_ptr createELFWriter(const CopyConfig &Config, + Object &Obj, Buffer &Buf, + ElfType OutputElfType) { + // Depending on the initial ELFT and OutputFormat we need a different Writer. + switch (OutputElfType) { + case ELFT_ELF32LE: + return std::make_unique>(Obj, Buf, !Config.StripSections, + Config.OnlyKeepDebug); + case ELFT_ELF64LE: + return std::make_unique>(Obj, Buf, !Config.StripSections, + Config.OnlyKeepDebug); + case ELFT_ELF32BE: + return std::make_unique>(Obj, Buf, !Config.StripSections, + Config.OnlyKeepDebug); + case ELFT_ELF64BE: + return std::make_unique>(Obj, Buf, !Config.StripSections, + Config.OnlyKeepDebug); + } + llvm_unreachable("Invalid output format"); +} + +static std::unique_ptr createWriter(const CopyConfig &Config, + Object &Obj, Buffer &Buf, + ElfType OutputElfType) { + switch (Config.OutputFormat) { + case FileFormat::Binary: + return std::make_unique(Obj, Buf); + case FileFormat::IHex: + return std::make_unique(Obj, Buf); + default: + return createELFWriter(Config, Obj, Buf, OutputElfType); + } +} + +template +static Expected> +findBuildID(const CopyConfig &Config, const object::ELFFile &In) { + auto PhdrsOrErr = In.program_headers(); + if (auto Err = PhdrsOrErr.takeError()) + return createFileError(Config.InputFilename, std::move(Err)); + + for (const auto &Phdr : *PhdrsOrErr) { + if (Phdr.p_type != PT_NOTE) + continue; + Error Err = Error::success(); + for (auto Note : In.notes(Phdr, Err)) + if (Note.getType() == NT_GNU_BUILD_ID && Note.getName() == ELF_NOTE_GNU) + return Note.getDesc(); + if (Err) + return createFileError(Config.InputFilename, std::move(Err)); + } + + return createFileError(Config.InputFilename, + createStringError(llvm::errc::invalid_argument, + "could not find build ID")); +} + +static Expected> +findBuildID(const CopyConfig &Config, const object::ELFObjectFileBase &In) { + if (auto *O = dyn_cast>(&In)) + return findBuildID(Config, *O->getELFFile()); + else if (auto *O = dyn_cast>(&In)) + return findBuildID(Config, *O->getELFFile()); + else if (auto *O = dyn_cast>(&In)) + return findBuildID(Config, *O->getELFFile()); + else if (auto *O = dyn_cast>(&In)) + return findBuildID(Config, *O->getELFFile()); + + llvm_unreachable("Bad file format"); +} + +template +static Error makeStringError(std::error_code EC, const Twine &Msg, + Ts &&... Args) { + std::string FullMsg = (EC.message() + ": " + Msg).str(); + return createStringError(EC, FullMsg.c_str(), std::forward(Args)...); +} + +#define MODEL_8 "%%%%%%%%" +#define MODEL_16 MODEL_8 MODEL_8 +#define MODEL_32 (MODEL_16 MODEL_16) + +static Error linkToBuildIdDir(const CopyConfig &Config, StringRef ToLink, + StringRef Suffix, + ArrayRef BuildIdBytes) { + SmallString<128> Path = Config.BuildIdLinkDir; + sys::path::append(Path, llvm::toHex(BuildIdBytes[0], /*LowerCase*/ true)); + if (auto EC = sys::fs::create_directories(Path)) + return createFileError( + Path.str(), + makeStringError(EC, "cannot create build ID link directory")); + + sys::path::append(Path, + llvm::toHex(BuildIdBytes.slice(1), /*LowerCase*/ true)); + Path += Suffix; + SmallString<128> TmpPath; + // create_hard_link races so we need to link to a temporary path but + // we want to make sure that we choose a filename that does not exist. + // By using 32 model characters we get 128-bits of entropy. It is + // unlikely that this string has ever existed before much less exists + // on this disk or in the current working directory. + // Additionally we prepend the original Path for debugging but also + // because it ensures that we're linking within a directory on the same + // partition on the same device which is critical. It has the added + // win of yet further decreasing the odds of a conflict. + sys::fs::createUniquePath(Twine(Path) + "-" + MODEL_32 + ".tmp", TmpPath, + /*MakeAbsolute*/ false); + if (auto EC = sys::fs::create_hard_link(ToLink, TmpPath)) { + Path.push_back('\0'); + return makeStringError(EC, "cannot link '%s' to '%s'", ToLink.data(), + Path.data()); + } + // We then atomically rename the link into place which will just move the + // link. If rename fails something is more seriously wrong so just return + // an error. + if (auto EC = sys::fs::rename(TmpPath, Path)) { + Path.push_back('\0'); + return makeStringError(EC, "cannot link '%s' to '%s'", ToLink.data(), + Path.data()); + } + // If `Path` was already a hard-link to the same underlying file then the + // temp file will be left so we need to remove it. Remove will not cause + // an error by default if the file is already gone so just blindly remove + // it rather than checking. + if (auto EC = sys::fs::remove(TmpPath)) { + TmpPath.push_back('\0'); + return makeStringError(EC, "could not remove '%s'", TmpPath.data()); + } + return Error::success(); +} + +static Error splitDWOToFile(const CopyConfig &Config, const Reader &Reader, + StringRef File, ElfType OutputElfType) { + auto DWOFile = Reader.create(false); + auto OnlyKeepDWOPred = [&DWOFile](const SectionBase &Sec) { + return onlyKeepDWOPred(*DWOFile, Sec); + }; + if (Error E = + DWOFile->removeSections(Config.AllowBrokenLinks, OnlyKeepDWOPred)) + return E; + if (Config.OutputArch) { + DWOFile->Machine = Config.OutputArch.getValue().EMachine; + DWOFile->OSABI = Config.OutputArch.getValue().OSABI; + } + FileBuffer FB(File); + auto Writer = createWriter(Config, *DWOFile, FB, OutputElfType); + if (Error E = Writer->finalize()) + return E; + return Writer->write(); +} + +static Error dumpSectionToFile(StringRef SecName, StringRef Filename, + Object &Obj) { + for (auto &Sec : Obj.sections()) { + if (Sec.Name == SecName) { + if (Sec.Type == SHT_NOBITS) + return createStringError(object_error::parse_failed, + "cannot dump section '%s': it has no contents", + SecName.str().c_str()); + Expected> BufferOrErr = + FileOutputBuffer::create(Filename, Sec.OriginalData.size()); + if (!BufferOrErr) + return BufferOrErr.takeError(); + std::unique_ptr Buf = std::move(*BufferOrErr); + std::copy(Sec.OriginalData.begin(), Sec.OriginalData.end(), + Buf->getBufferStart()); + if (Error E = Buf->commit()) + return E; + return Error::success(); + } + } + return createStringError(object_error::parse_failed, "section '%s' not found", + SecName.str().c_str()); +} + +static bool isCompressable(const SectionBase &Sec) { + return !(Sec.Flags & ELF::SHF_COMPRESSED) && + StringRef(Sec.Name).startswith(".debug"); +} + +static void replaceDebugSections( + Object &Obj, SectionPred &RemovePred, + std::function shouldReplace, + std::function addSection) { + // Build a list of the debug sections we are going to replace. + // We can't call `addSection` while iterating over sections, + // because it would mutate the sections array. + SmallVector ToReplace; + for (auto &Sec : Obj.sections()) + if (shouldReplace(Sec)) + ToReplace.push_back(&Sec); + + // Build a mapping from original section to a new one. + DenseMap FromTo; + for (SectionBase *S : ToReplace) + FromTo[S] = addSection(S); + + // Now we want to update the target sections of relocation + // sections. Also we will update the relocations themselves + // to update the symbol references. + for (auto &Sec : Obj.sections()) + Sec.replaceSectionReferences(FromTo); + + RemovePred = [shouldReplace, RemovePred](const SectionBase &Sec) { + return shouldReplace(Sec) || RemovePred(Sec); + }; +} + +static bool isUnneededSymbol(const Symbol &Sym) { + return !Sym.Referenced && + (Sym.Binding == STB_LOCAL || Sym.getShndx() == SHN_UNDEF) && + Sym.Type != STT_SECTION; +} + +static Error updateAndRemoveSymbols(const CopyConfig &Config, Object &Obj) { + // TODO: update or remove symbols only if there is an option that affects + // them. + if (!Obj.SymbolTable) + return Error::success(); + + Obj.SymbolTable->updateSymbols([&](Symbol &Sym) { + // Common and undefined symbols don't make sense as local symbols, and can + // even cause crashes if we localize those, so skip them. + if (!Sym.isCommon() && Sym.getShndx() != SHN_UNDEF && + ((Config.LocalizeHidden && + (Sym.Visibility == STV_HIDDEN || Sym.Visibility == STV_INTERNAL)) || + Config.SymbolsToLocalize.matches(Sym.Name))) + Sym.Binding = STB_LOCAL; + + // Note: these two globalize flags have very similar names but different + // meanings: + // + // --globalize-symbol: promote a symbol to global + // --keep-global-symbol: all symbols except for these should be made local + // + // If --globalize-symbol is specified for a given symbol, it will be + // global in the output file even if it is not included via + // --keep-global-symbol. Because of that, make sure to check + // --globalize-symbol second. + if (!Config.SymbolsToKeepGlobal.empty() && + !Config.SymbolsToKeepGlobal.matches(Sym.Name) && + Sym.getShndx() != SHN_UNDEF) + Sym.Binding = STB_LOCAL; + + if (Config.SymbolsToGlobalize.matches(Sym.Name) && + Sym.getShndx() != SHN_UNDEF) + Sym.Binding = STB_GLOBAL; + + if (Config.SymbolsToWeaken.matches(Sym.Name) && Sym.Binding == STB_GLOBAL) + Sym.Binding = STB_WEAK; + + if (Config.Weaken && Sym.Binding == STB_GLOBAL && + Sym.getShndx() != SHN_UNDEF) + Sym.Binding = STB_WEAK; + + const auto I = Config.SymbolsToRename.find(Sym.Name); + if (I != Config.SymbolsToRename.end()) + Sym.Name = std::string(I->getValue()); + + if (!Config.SymbolsPrefix.empty() && Sym.Type != STT_SECTION) + Sym.Name = (Config.SymbolsPrefix + Sym.Name).str(); + }); + + // The purpose of this loop is to mark symbols referenced by sections + // (like GroupSection or RelocationSection). This way, we know which + // symbols are still 'needed' and which are not. + if (Config.StripUnneeded || !Config.UnneededSymbolsToRemove.empty() || + !Config.OnlySection.empty()) { + for (SectionBase &Sec : Obj.sections()) + Sec.markSymbols(); + } + + auto RemoveSymbolsPred = [&](const Symbol &Sym) { + if (Config.SymbolsToKeep.matches(Sym.Name) || + (Config.KeepFileSymbols && Sym.Type == STT_FILE)) + return false; + + if ((Config.DiscardMode == DiscardType::All || + (Config.DiscardMode == DiscardType::Locals && + StringRef(Sym.Name).startswith(".L"))) && + Sym.Binding == STB_LOCAL && Sym.getShndx() != SHN_UNDEF && + Sym.Type != STT_FILE && Sym.Type != STT_SECTION) + return true; + + if (Config.StripAll || Config.StripAllGNU) + return true; + + if (Config.StripDebug && Sym.Type == STT_FILE) + return true; + + if (Config.SymbolsToRemove.matches(Sym.Name)) + return true; + + if ((Config.StripUnneeded || + Config.UnneededSymbolsToRemove.matches(Sym.Name)) && + (!Obj.isRelocatable() || isUnneededSymbol(Sym))) + return true; + + // We want to remove undefined symbols if all references have been stripped. + if (!Config.OnlySection.empty() && !Sym.Referenced && + Sym.getShndx() == SHN_UNDEF) + return true; + + return false; + }; + + return Obj.removeSymbols(RemoveSymbolsPred); +} + +static Error replaceAndRemoveSections(const CopyConfig &Config, Object &Obj) { + SectionPred RemovePred = [](const SectionBase &) { return false; }; + + // Removes: + if (!Config.ToRemove.empty()) { + RemovePred = [&Config](const SectionBase &Sec) { + return Config.ToRemove.matches(Sec.Name); + }; + } + + if (Config.StripDWO || !Config.SplitDWO.empty()) + RemovePred = [RemovePred](const SectionBase &Sec) { + return isDWOSection(Sec) || RemovePred(Sec); + }; + + if (Config.ExtractDWO) + RemovePred = [RemovePred, &Obj](const SectionBase &Sec) { + return onlyKeepDWOPred(Obj, Sec) || RemovePred(Sec); + }; + + if (Config.StripAllGNU) + RemovePred = [RemovePred, &Obj](const SectionBase &Sec) { + if (RemovePred(Sec)) + return true; + if ((Sec.Flags & SHF_ALLOC) != 0) + return false; + if (&Sec == Obj.SectionNames) + return false; + switch (Sec.Type) { + case SHT_SYMTAB: + case SHT_REL: + case SHT_RELA: + case SHT_STRTAB: + return true; + } + return isDebugSection(Sec); + }; + + if (Config.StripSections) { + RemovePred = [RemovePred](const SectionBase &Sec) { + return RemovePred(Sec) || Sec.ParentSegment == nullptr; + }; + } + + if (Config.StripDebug || Config.StripUnneeded) { + RemovePred = [RemovePred](const SectionBase &Sec) { + return RemovePred(Sec) || isDebugSection(Sec); + }; + } + + if (Config.StripNonAlloc) + RemovePred = [RemovePred, &Obj](const SectionBase &Sec) { + if (RemovePred(Sec)) + return true; + if (&Sec == Obj.SectionNames) + return false; + return (Sec.Flags & SHF_ALLOC) == 0 && Sec.ParentSegment == nullptr; + }; + + if (Config.StripAll) + RemovePred = [RemovePred, &Obj](const SectionBase &Sec) { + if (RemovePred(Sec)) + return true; + if (&Sec == Obj.SectionNames) + return false; + if (StringRef(Sec.Name).startswith(".gnu.warning")) + return false; + // We keep the .ARM.attribute section to maintain compatibility + // with Debian derived distributions. This is a bug in their + // patchset as documented here: + // https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=943798 + if (Sec.Type == SHT_ARM_ATTRIBUTES) + return false; + if (Sec.ParentSegment != nullptr) + return false; + return (Sec.Flags & SHF_ALLOC) == 0; + }; + + if (Config.ExtractPartition || Config.ExtractMainPartition) { + RemovePred = [RemovePred](const SectionBase &Sec) { + if (RemovePred(Sec)) + return true; + if (Sec.Type == SHT_LLVM_PART_EHDR || Sec.Type == SHT_LLVM_PART_PHDR) + return true; + return (Sec.Flags & SHF_ALLOC) != 0 && !Sec.ParentSegment; + }; + } + + // Explicit copies: + if (!Config.OnlySection.empty()) { + RemovePred = [&Config, RemovePred, &Obj](const SectionBase &Sec) { + // Explicitly keep these sections regardless of previous removes. + if (Config.OnlySection.matches(Sec.Name)) + return false; + + // Allow all implicit removes. + if (RemovePred(Sec)) + return true; + + // Keep special sections. + if (Obj.SectionNames == &Sec) + return false; + if (Obj.SymbolTable == &Sec || + (Obj.SymbolTable && Obj.SymbolTable->getStrTab() == &Sec)) + return false; + + // Remove everything else. + return true; + }; + } + + if (!Config.KeepSection.empty()) { + RemovePred = [&Config, RemovePred](const SectionBase &Sec) { + // Explicitly keep these sections regardless of previous removes. + if (Config.KeepSection.matches(Sec.Name)) + return false; + // Otherwise defer to RemovePred. + return RemovePred(Sec); + }; + } + + // This has to be the last predicate assignment. + // If the option --keep-symbol has been specified + // and at least one of those symbols is present + // (equivalently, the updated symbol table is not empty) + // the symbol table and the string table should not be removed. + if ((!Config.SymbolsToKeep.empty() || Config.KeepFileSymbols) && + Obj.SymbolTable && !Obj.SymbolTable->empty()) { + RemovePred = [&Obj, RemovePred](const SectionBase &Sec) { + if (&Sec == Obj.SymbolTable || &Sec == Obj.SymbolTable->getStrTab()) + return false; + return RemovePred(Sec); + }; + } + + if (Config.CompressionType != DebugCompressionType::None) + replaceDebugSections(Obj, RemovePred, + [Config](const SectionBase &S) { + return isCompressable(S) && + Config.NewDebugSections.count(S.Name) == 0; + }, + [&Config, &Obj](const SectionBase *S) { + return &Obj.addSection( + *S, Config.CompressionType); + }); + else if (Config.DecompressDebugSections) + replaceDebugSections( + Obj, RemovePred, + [](const SectionBase &S) { return isa(&S); }, + [&Obj](const SectionBase *S) { + auto CS = cast(S); + return &Obj.addSection(*CS); + }); + + return Obj.removeSections(Config.AllowBrokenLinks, RemovePred); +} + +// This function handles the high level operations of GNU objcopy including +// handling command line options. It's important to outline certain properties +// we expect to hold of the command line operations. Any operation that "keeps" +// should keep regardless of a remove. Additionally any removal should respect +// any previous removals. Lastly whether or not something is removed shouldn't +// depend a) on the order the options occur in or b) on some opaque priority +// system. The only priority is that keeps/copies overrule removes. +static Error handleArgs(const CopyConfig &Config, Object &Obj, + const Reader &Reader, ElfType OutputElfType) { + if (Config.StripSwiftSymbols) + return createStringError(llvm::errc::invalid_argument, + "option not supported by llvm-objcopy for ELF"); + if (!Config.SplitDWO.empty()) + if (Error E = + splitDWOToFile(Config, Reader, Config.SplitDWO, OutputElfType)) + return E; + + if (Config.OutputArch) { + Obj.Machine = Config.OutputArch.getValue().EMachine; + Obj.OSABI = Config.OutputArch.getValue().OSABI; + } + + // Dump sections before add/remove for compatibility with GNU objcopy. + for (StringRef Flag : Config.DumpSection) { + StringRef SectionName; + StringRef FileName; + std::tie(SectionName, FileName) = Flag.split('='); + if (Error E = dumpSectionToFile(SectionName, FileName, Obj)) + return E; + } + + // It is important to remove the sections first. For example, we want to + // remove the relocation sections before removing the symbols. That allows + // us to avoid reporting the inappropriate errors about removing symbols + // named in relocations. + if (Error E = replaceAndRemoveSections(Config, Obj)) + return E; + + if (Error E = updateAndRemoveSymbols(Config, Obj)) + return E; + + if (!Config.SectionsToRename.empty()) { + for (SectionBase &Sec : Obj.sections()) { + const auto Iter = Config.SectionsToRename.find(Sec.Name); + if (Iter != Config.SectionsToRename.end()) { + const SectionRename &SR = Iter->second; + Sec.Name = std::string(SR.NewName); + if (SR.NewFlags.hasValue()) + setSectionFlagsAndType(Sec, SR.NewFlags.getValue()); + } + } + } + + // Add a prefix to allocated sections and their relocation sections. This + // should be done after renaming the section by Config.SectionToRename to + // imitate the GNU objcopy behavior. + if (!Config.AllocSectionsPrefix.empty()) { + DenseSet PrefixedSections; + for (SectionBase &Sec : Obj.sections()) { + if (Sec.Flags & SHF_ALLOC) { + Sec.Name = (Config.AllocSectionsPrefix + Sec.Name).str(); + PrefixedSections.insert(&Sec); + } else if (auto *RelocSec = dyn_cast(&Sec)) { + // Rename relocation sections associated to the allocated sections. + // For example, if we rename .text to .prefix.text, we also rename + // .rel.text to .rel.prefix.text. + // + // Dynamic relocation sections (SHT_REL[A] with SHF_ALLOC) are handled + // above, e.g., .rela.plt is renamed to .prefix.rela.plt, not + // .rela.prefix.plt since GNU objcopy does so. + const SectionBase *TargetSec = RelocSec->getSection(); + if (TargetSec && (TargetSec->Flags & SHF_ALLOC)) { + StringRef prefix; + switch (Sec.Type) { + case SHT_REL: + prefix = ".rel"; + break; + case SHT_RELA: + prefix = ".rela"; + break; + default: + llvm_unreachable("not a relocation section"); + } + + // If the relocation section comes *after* the target section, we + // don't add Config.AllocSectionsPrefix because we've already added + // the prefix to TargetSec->Name. Otherwise, if the relocation + // section comes *before* the target section, we add the prefix. + if (PrefixedSections.count(TargetSec)) + Sec.Name = (prefix + TargetSec->Name).str(); + else + Sec.Name = + (prefix + Config.AllocSectionsPrefix + TargetSec->Name).str(); + } + } + } + } + + if (!Config.SetSectionAlignment.empty()) { + for (SectionBase &Sec : Obj.sections()) { + auto I = Config.SetSectionAlignment.find(Sec.Name); + if (I != Config.SetSectionAlignment.end()) + Sec.Align = I->second; + } + } + + if (!Config.SetSectionFlags.empty()) { + for (auto &Sec : Obj.sections()) { + const auto Iter = Config.SetSectionFlags.find(Sec.Name); + if (Iter != Config.SetSectionFlags.end()) { + const SectionFlagsUpdate &SFU = Iter->second; + setSectionFlagsAndType(Sec, SFU.NewFlags); + } + } + } + + if (Config.OnlyKeepDebug) + for (auto &Sec : Obj.sections()) + if (Sec.Flags & SHF_ALLOC && Sec.Type != SHT_NOTE) + Sec.Type = SHT_NOBITS; + + for (const auto &Flag : Config.AddSection) { + std::pair SecPair = Flag.split("="); + StringRef SecName = SecPair.first; + StringRef File = SecPair.second; + ErrorOr> BufOrErr = + MemoryBuffer::getFile(File); + if (!BufOrErr) + return createFileError(File, errorCodeToError(BufOrErr.getError())); + std::unique_ptr Buf = std::move(*BufOrErr); + ArrayRef Data( + reinterpret_cast(Buf->getBufferStart()), + Buf->getBufferSize()); + OwnedDataSection &NewSection = + Obj.addSection(SecName, Data); + if (SecName.startswith(".note") && SecName != ".note.GNU-stack") + NewSection.Type = SHT_NOTE; + } + + for (const auto &Sec : Config.NewDebugSections) { + ArrayRef DataBits((const uint8_t *)Sec.getValue().data(), + Sec.getValue().size()); + Section NewSection(DataBits); + NewSection.Name = std::string(Sec.getKey().begin(), Sec.getKey().end()); + NewSection.Type = NewSection.OriginalType = ELF::SHT_PROGBITS; + NewSection.Size = DataBits.size(); + NewSection.OriginalOffset = std::numeric_limits::max(); + NewSection.OriginalData = DataBits; + + if (Config.CompressionType != DebugCompressionType::None) + Obj.addSection(NewSection, Config.CompressionType); + else + Obj.addSection
(NewSection); + } + + if (!Config.AddGnuDebugLink.empty()) + Obj.addSection(Config.AddGnuDebugLink, + Config.GnuDebugLinkCRC32); + + // If the symbol table was previously removed, we need to create a new one + // before adding new symbols. + if (!Obj.SymbolTable && Config.ELF && !Config.ELF->SymbolsToAdd.empty()) { + Obj.addNewSymbolTable(); + } + + if (Config.ELF) + for (const NewSymbolInfo &SI : Config.ELF->SymbolsToAdd) { + SectionBase *Sec = Obj.findSection(SI.SectionName); + uint64_t Value = Sec ? Sec->Addr + SI.Value : SI.Value; + Obj.SymbolTable->addSymbol( + SI.SymbolName, SI.Bind, SI.Type, Sec, Value, SI.Visibility, + Sec ? (uint16_t)SYMBOL_SIMPLE_INDEX : (uint16_t)SHN_ABS, 0); + } + + if (Config.EntryExpr) + Obj.Entry = Config.EntryExpr(Obj.Entry); + return Error::success(); +} + +static Error writeOutput(const CopyConfig &Config, Object &Obj, Buffer &Out, + ElfType OutputElfType) { + std::unique_ptr Writer = + createWriter(Config, Obj, Out, OutputElfType); + if (Error E = Writer->finalize()) + return E; + return Writer->write(); +} + +Error executeObjcopyOnIHex(const CopyConfig &Config, MemoryBuffer &In, + Buffer &Out) { + IHexReader Reader(&In); + std::unique_ptr Obj = Reader.create(true); + const ElfType OutputElfType = + getOutputElfType(Config.OutputArch.getValueOr(MachineInfo())); + if (Error E = handleArgs(Config, *Obj, Reader, OutputElfType)) + return E; + return writeOutput(Config, *Obj, Out, OutputElfType); +} + +Error executeObjcopyOnRawBinary(const CopyConfig &Config, MemoryBuffer &In, + Buffer &Out) { + uint8_t NewSymbolVisibility = + Config.ELF->NewSymbolVisibility.getValueOr((uint8_t)ELF::STV_DEFAULT); + BinaryReader Reader(&In, NewSymbolVisibility); + std::unique_ptr Obj = Reader.create(true); + + // Prefer OutputArch (-O) if set, otherwise fallback to BinaryArch + // (-B). + const ElfType OutputElfType = + getOutputElfType(Config.OutputArch.getValueOr(MachineInfo())); + if (Error E = handleArgs(Config, *Obj, Reader, OutputElfType)) + return E; + return writeOutput(Config, *Obj, Out, OutputElfType); +} + +Error executeObjcopyOnBinary(const CopyConfig &Config, + object::ELFObjectFileBase &In, Buffer &Out) { + ELFReader Reader(&In, Config.ExtractPartition); + std::unique_ptr Obj = Reader.create(!Config.SymbolsToAdd.empty()); + // Prefer OutputArch (-O) if set, otherwise infer it from the input. + const ElfType OutputElfType = + Config.OutputArch ? getOutputElfType(Config.OutputArch.getValue()) + : getOutputElfType(In); + ArrayRef BuildIdBytes; + + if (!Config.BuildIdLinkDir.empty()) { + auto BuildIdBytesOrErr = findBuildID(Config, In); + if (auto E = BuildIdBytesOrErr.takeError()) + return E; + BuildIdBytes = *BuildIdBytesOrErr; + + if (BuildIdBytes.size() < 2) + return createFileError( + Config.InputFilename, + createStringError(object_error::parse_failed, + "build ID is smaller than two bytes")); + } + + if (!Config.BuildIdLinkDir.empty() && Config.BuildIdLinkInput) + if (Error E = + linkToBuildIdDir(Config, Config.InputFilename, + Config.BuildIdLinkInput.getValue(), BuildIdBytes)) + return E; + + if (Error E = handleArgs(Config, *Obj, Reader, OutputElfType)) + return createFileError(Config.InputFilename, std::move(E)); + + if (Error E = writeOutput(Config, *Obj, Out, OutputElfType)) + return createFileError(Config.InputFilename, std::move(E)); + if (!Config.BuildIdLinkDir.empty() && Config.BuildIdLinkOutput) + if (Error E = + linkToBuildIdDir(Config, Config.OutputFilename, + Config.BuildIdLinkOutput.getValue(), BuildIdBytes)) + return createFileError(Config.OutputFilename, std::move(E)); + + return Error::success(); +} + +} // end namespace elf +} // end namespace objectcopy +} // end namespace llvm diff --git a/llvm/lib/ObjectCopy/ELF/Object.cpp b/llvm/lib/ObjectCopy/ELF/Object.cpp new file mode 100644 --- /dev/null +++ b/llvm/lib/ObjectCopy/ELF/Object.cpp @@ -0,0 +1,2397 @@ +//===- Object.cpp ---------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "llvm/ObjectCopy/ELF/Object.h" +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/ADT/Twine.h" +#include "llvm/ADT/iterator_range.h" +#include "llvm/BinaryFormat/ELF.h" +#include "llvm/MC/MCTargetOptions.h" +#include "llvm/Object/ELFObjectFile.h" +#include "llvm/Support/Compression.h" +#include "llvm/Support/Endian.h" +#include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/FileOutputBuffer.h" +#include "llvm/Support/Path.h" +#include +#include +#include +#include +#include +#include +#include + +namespace llvm { +namespace objectcopy { +namespace elf { + +using namespace object; +using namespace ELF; + +template void ELFWriter::writePhdr(const Segment &Seg) { + uint8_t *B = Buf.getBufferStart() + Obj.ProgramHdrSegment.Offset + + Seg.Index * sizeof(Elf_Phdr); + Elf_Phdr &Phdr = *reinterpret_cast(B); + Phdr.p_type = Seg.Type; + Phdr.p_flags = Seg.Flags; + Phdr.p_offset = Seg.Offset; + Phdr.p_vaddr = Seg.VAddr; + Phdr.p_paddr = Seg.PAddr; + Phdr.p_filesz = Seg.FileSize; + Phdr.p_memsz = Seg.MemSize; + Phdr.p_align = Seg.Align; +} + +Error SectionBase::removeSectionReferences( + bool AllowBrokenLinks, function_ref ToRemove) { + return Error::success(); +} + +Error SectionBase::removeSymbols(function_ref ToRemove) { + return Error::success(); +} + +void SectionBase::initialize(SectionTableRef SecTable) {} +void SectionBase::finalize() {} +void SectionBase::markSymbols() {} +void SectionBase::replaceSectionReferences( + const DenseMap &) {} +void SectionBase::onRemove() {} + +template void ELFWriter::writeShdr(const SectionBase &Sec) { + uint8_t *B = Buf.getBufferStart() + Sec.HeaderOffset; + Elf_Shdr &Shdr = *reinterpret_cast(B); + Shdr.sh_name = Sec.NameIndex; + Shdr.sh_type = Sec.Type; + Shdr.sh_flags = Sec.Flags; + Shdr.sh_addr = Sec.Addr; + Shdr.sh_offset = Sec.Offset; + Shdr.sh_size = Sec.Size; + Shdr.sh_link = Sec.Link; + Shdr.sh_info = Sec.Info; + Shdr.sh_addralign = Sec.Align; + Shdr.sh_entsize = Sec.EntrySize; +} + +template void ELFSectionSizer::visit(Section &Sec) {} + +template +void ELFSectionSizer::visit(OwnedDataSection &Sec) {} + +template +void ELFSectionSizer::visit(StringTableSection &Sec) {} + +template +void ELFSectionSizer::visit(DynamicRelocationSection &Sec) {} + +template +void ELFSectionSizer::visit(SymbolTableSection &Sec) { + Sec.EntrySize = sizeof(Elf_Sym); + Sec.Size = Sec.Symbols.size() * Sec.EntrySize; + // Align to the largest field in Elf_Sym. + Sec.Align = ELFT::Is64Bits ? sizeof(Elf_Xword) : sizeof(Elf_Word); +} + +template +void ELFSectionSizer::visit(RelocationSection &Sec) { + Sec.EntrySize = Sec.Type == SHT_REL ? sizeof(Elf_Rel) : sizeof(Elf_Rela); + Sec.Size = Sec.Relocations.size() * Sec.EntrySize; + // Align to the largest field in Elf_Rel(a). + Sec.Align = ELFT::Is64Bits ? sizeof(Elf_Xword) : sizeof(Elf_Word); +} + +template +void ELFSectionSizer::visit(GnuDebugLinkSection &Sec) {} + +template void ELFSectionSizer::visit(GroupSection &Sec) { + Sec.Size = sizeof(Elf_Word) + Sec.GroupMembers.size() * sizeof(Elf_Word); +} + +template +void ELFSectionSizer::visit(SectionIndexSection &Sec) {} + +template +void ELFSectionSizer::visit(CompressedSection &Sec) {} + +template +void ELFSectionSizer::visit(DecompressedSection &Sec) {} + +void BinarySectionWriter::visit(const SectionIndexSection &Sec) { + error("cannot write symbol section index table '" + Sec.Name + "' "); +} + +void BinarySectionWriter::visit(const SymbolTableSection &Sec) { + error("cannot write symbol table '" + Sec.Name + "' out to binary"); +} + +void BinarySectionWriter::visit(const RelocationSection &Sec) { + error("cannot write relocation section '" + Sec.Name + "' out to binary"); +} + +void BinarySectionWriter::visit(const GnuDebugLinkSection &Sec) { + error("cannot write '" + Sec.Name + "' out to binary"); +} + +void BinarySectionWriter::visit(const GroupSection &Sec) { + error("cannot write '" + Sec.Name + "' out to binary"); +} + +void SectionWriter::visit(const Section &Sec) { + if (Sec.Type != SHT_NOBITS) + llvm::copy(Sec.Contents, Out.getBufferStart() + Sec.Offset); +} + +static bool addressOverflows32bit(uint64_t Addr) { + // Sign extended 32 bit addresses (e.g 0xFFFFFFFF80000000) are ok + return Addr > UINT32_MAX && Addr + 0x80000000 > UINT32_MAX; +} + +template static T checkedGetHex(StringRef S) { + T Value; + bool Fail = S.getAsInteger(16, Value); + assert(!Fail); + (void)Fail; + return Value; +} + +// Fills exactly Len bytes of buffer with hexadecimal characters +// representing value 'X' +template +static Iterator utohexstr(T X, Iterator It, size_t Len) { + // Fill range with '0' + std::fill(It, It + Len, '0'); + + for (long I = Len - 1; I >= 0; --I) { + unsigned char Mod = static_cast(X) & 15; + *(It + I) = hexdigit(Mod, false); + X >>= 4; + } + assert(X == 0); + return It + Len; +} + +uint8_t IHexRecord::getChecksum(StringRef S) { + assert((S.size() & 1) == 0); + uint8_t Checksum = 0; + while (!S.empty()) { + Checksum += checkedGetHex(S.take_front(2)); + S = S.drop_front(2); + } + return -Checksum; +} + +IHexLineData IHexRecord::getLine(uint8_t Type, uint16_t Addr, + ArrayRef Data) { + IHexLineData Line(getLineLength(Data.size())); + assert(Line.size()); + auto Iter = Line.begin(); + *Iter++ = ':'; + Iter = utohexstr(Data.size(), Iter, 2); + Iter = utohexstr(Addr, Iter, 4); + Iter = utohexstr(Type, Iter, 2); + for (uint8_t X : Data) + Iter = utohexstr(X, Iter, 2); + StringRef S(Line.data() + 1, std::distance(Line.begin() + 1, Iter)); + Iter = utohexstr(getChecksum(S), Iter, 2); + *Iter++ = '\r'; + *Iter++ = '\n'; + assert(Iter == Line.end()); + return Line; +} + +static Error checkRecord(const IHexRecord &R) { + switch (R.Type) { + case IHexRecord::Data: + if (R.HexData.size() == 0) + return createStringError( + errc::invalid_argument, + "zero data length is not allowed for data records"); + break; + case IHexRecord::EndOfFile: + break; + case IHexRecord::SegmentAddr: + // 20-bit segment address. Data length must be 2 bytes + // (4 bytes in hex) + if (R.HexData.size() != 4) + return createStringError( + errc::invalid_argument, + "segment address data should be 2 bytes in size"); + break; + case IHexRecord::StartAddr80x86: + case IHexRecord::StartAddr: + if (R.HexData.size() != 8) + return createStringError(errc::invalid_argument, + "start address data should be 4 bytes in size"); + // According to Intel HEX specification '03' record + // only specifies the code address within the 20-bit + // segmented address space of the 8086/80186. This + // means 12 high order bits should be zeroes. + if (R.Type == IHexRecord::StartAddr80x86 && + R.HexData.take_front(3) != "000") + return createStringError(errc::invalid_argument, + "start address exceeds 20 bit for 80x86"); + break; + case IHexRecord::ExtendedAddr: + // 16-31 bits of linear base address + if (R.HexData.size() != 4) + return createStringError( + errc::invalid_argument, + "extended address data should be 2 bytes in size"); + break; + default: + // Unknown record type + return createStringError(errc::invalid_argument, "unknown record type: %u", + static_cast(R.Type)); + } + return Error::success(); +} + +// Checks that IHEX line contains valid characters. +// This allows converting hexadecimal data to integers +// without extra verification. +static Error checkChars(StringRef Line) { + assert(!Line.empty()); + if (Line[0] != ':') + return createStringError(errc::invalid_argument, + "missing ':' in the beginning of line."); + + for (size_t Pos = 1; Pos < Line.size(); ++Pos) + if (hexDigitValue(Line[Pos]) == -1U) + return createStringError(errc::invalid_argument, + "invalid character at position %zu.", Pos + 1); + return Error::success(); +} + +Expected IHexRecord::parse(StringRef Line) { + assert(!Line.empty()); + + // ':' + Length + Address + Type + Checksum with empty data ':LLAAAATTCC' + if (Line.size() < 11) + return createStringError(errc::invalid_argument, + "line is too short: %zu chars.", Line.size()); + + if (Error E = checkChars(Line)) + return std::move(E); + + IHexRecord Rec; + size_t DataLen = checkedGetHex(Line.substr(1, 2)); + if (Line.size() != getLength(DataLen)) + return createStringError(errc::invalid_argument, + "invalid line length %zu (should be %zu)", + Line.size(), getLength(DataLen)); + + Rec.Addr = checkedGetHex(Line.substr(3, 4)); + Rec.Type = checkedGetHex(Line.substr(7, 2)); + Rec.HexData = Line.substr(9, DataLen * 2); + + if (getChecksum(Line.drop_front(1)) != 0) + return createStringError(errc::invalid_argument, "incorrect checksum."); + if (Error E = checkRecord(Rec)) + return std::move(E); + return Rec; +} + +static uint64_t sectionPhysicalAddr(const SectionBase *Sec) { + Segment *Seg = Sec->ParentSegment; + if (Seg && Seg->Type != ELF::PT_LOAD) + Seg = nullptr; + return Seg ? Seg->PAddr + Sec->OriginalOffset - Seg->OriginalOffset + : Sec->Addr; +} + +void IHexSectionWriterBase::writeSection(const SectionBase *Sec, + ArrayRef Data) { + assert(Data.size() == Sec->Size); + const uint32_t ChunkSize = 16; + uint32_t Addr = sectionPhysicalAddr(Sec) & 0xFFFFFFFFU; + while (!Data.empty()) { + uint64_t DataSize = std::min(Data.size(), ChunkSize); + if (Addr > SegmentAddr + BaseAddr + 0xFFFFU) { + if (Addr > 0xFFFFFU) { + // Write extended address record, zeroing segment address + // if needed. + if (SegmentAddr != 0) + SegmentAddr = writeSegmentAddr(0U); + BaseAddr = writeBaseAddr(Addr); + } else { + // We can still remain 16-bit + SegmentAddr = writeSegmentAddr(Addr); + } + } + uint64_t SegOffset = Addr - BaseAddr - SegmentAddr; + assert(SegOffset <= 0xFFFFU); + DataSize = std::min(DataSize, 0x10000U - SegOffset); + writeData(0, SegOffset, Data.take_front(DataSize)); + Addr += DataSize; + Data = Data.drop_front(DataSize); + } +} + +uint64_t IHexSectionWriterBase::writeSegmentAddr(uint64_t Addr) { + assert(Addr <= 0xFFFFFU); + uint8_t Data[] = {static_cast((Addr & 0xF0000U) >> 12), 0}; + writeData(2, 0, Data); + return Addr & 0xF0000U; +} + +uint64_t IHexSectionWriterBase::writeBaseAddr(uint64_t Addr) { + assert(Addr <= 0xFFFFFFFFU); + uint64_t Base = Addr & 0xFFFF0000U; + uint8_t Data[] = {static_cast(Base >> 24), + static_cast((Base >> 16) & 0xFF)}; + writeData(4, 0, Data); + return Base; +} + +void IHexSectionWriterBase::writeData(uint8_t Type, uint16_t Addr, + ArrayRef Data) { + Offset += IHexRecord::getLineLength(Data.size()); +} + +void IHexSectionWriterBase::visit(const Section &Sec) { + writeSection(&Sec, Sec.Contents); +} + +void IHexSectionWriterBase::visit(const OwnedDataSection &Sec) { + writeSection(&Sec, Sec.Data); +} + +void IHexSectionWriterBase::visit(const StringTableSection &Sec) { + // Check that sizer has already done its work + assert(Sec.Size == Sec.StrTabBuilder.getSize()); + // We are free to pass an invalid pointer to writeSection as long + // as we don't actually write any data. The real writer class has + // to override this method . + writeSection(&Sec, {nullptr, static_cast(Sec.Size)}); +} + +void IHexSectionWriterBase::visit(const DynamicRelocationSection &Sec) { + writeSection(&Sec, Sec.Contents); +} + +void IHexSectionWriter::writeData(uint8_t Type, uint16_t Addr, + ArrayRef Data) { + IHexLineData HexData = IHexRecord::getLine(Type, Addr, Data); + memcpy(Out.getBufferStart() + Offset, HexData.data(), HexData.size()); + Offset += HexData.size(); +} + +void IHexSectionWriter::visit(const StringTableSection &Sec) { + assert(Sec.Size == Sec.StrTabBuilder.getSize()); + std::vector Data(Sec.Size); + Sec.StrTabBuilder.write(Data.data()); + writeSection(&Sec, Data); +} + +void Section::accept(SectionVisitor &Visitor) const { Visitor.visit(*this); } + +void Section::accept(MutableSectionVisitor &Visitor) { Visitor.visit(*this); } + +void SectionWriter::visit(const OwnedDataSection &Sec) { + llvm::copy(Sec.Data, Out.getBufferStart() + Sec.Offset); +} + +static constexpr std::array ZlibGnuMagic = {{'Z', 'L', 'I', 'B'}}; + +static bool isDataGnuCompressed(ArrayRef Data) { + return Data.size() > ZlibGnuMagic.size() && + std::equal(ZlibGnuMagic.begin(), ZlibGnuMagic.end(), Data.data()); +} + +template +static std::tuple +getDecompressedSizeAndAlignment(ArrayRef Data) { + const bool IsGnuDebug = isDataGnuCompressed(Data); + const uint64_t DecompressedSize = + IsGnuDebug + ? support::endian::read64be(Data.data() + ZlibGnuMagic.size()) + : reinterpret_cast *>(Data.data())->ch_size; + const uint64_t DecompressedAlign = + IsGnuDebug ? 1 + : reinterpret_cast *>(Data.data()) + ->ch_addralign; + + return std::make_tuple(DecompressedSize, DecompressedAlign); +} + +template +void ELFSectionWriter::visit(const DecompressedSection &Sec) { + const size_t DataOffset = isDataGnuCompressed(Sec.OriginalData) + ? (ZlibGnuMagic.size() + sizeof(Sec.Size)) + : sizeof(Elf_Chdr_Impl); + + StringRef CompressedContent( + reinterpret_cast(Sec.OriginalData.data()) + DataOffset, + Sec.OriginalData.size() - DataOffset); + + SmallVector DecompressedContent; + if (Error E = zlib::uncompress(CompressedContent, DecompressedContent, + static_cast(Sec.Size))) + reportError(Sec.Name, std::move(E)); + + uint8_t *Buf = Out.getBufferStart() + Sec.Offset; + std::copy(DecompressedContent.begin(), DecompressedContent.end(), Buf); +} + +void BinarySectionWriter::visit(const DecompressedSection &Sec) { + error("cannot write compressed section '" + Sec.Name + "' "); +} + +void DecompressedSection::accept(SectionVisitor &Visitor) const { + Visitor.visit(*this); +} + +void DecompressedSection::accept(MutableSectionVisitor &Visitor) { + Visitor.visit(*this); +} + +void OwnedDataSection::accept(SectionVisitor &Visitor) const { + Visitor.visit(*this); +} + +void OwnedDataSection::accept(MutableSectionVisitor &Visitor) { + Visitor.visit(*this); +} + +void OwnedDataSection::appendHexData(StringRef HexData) { + assert((HexData.size() & 1) == 0); + while (!HexData.empty()) { + Data.push_back(checkedGetHex(HexData.take_front(2))); + HexData = HexData.drop_front(2); + } + Size = Data.size(); +} + +void BinarySectionWriter::visit(const CompressedSection &Sec) { + error("cannot write compressed section '" + Sec.Name + "' "); +} + +template +void ELFSectionWriter::visit(const CompressedSection &Sec) { + uint8_t *Buf = Out.getBufferStart() + Sec.Offset; + if (Sec.CompressionType == DebugCompressionType::None) { + std::copy(Sec.OriginalData.begin(), Sec.OriginalData.end(), Buf); + return; + } + + if (Sec.CompressionType == DebugCompressionType::GNU) { + const char *Magic = "ZLIB"; + memcpy(Buf, Magic, strlen(Magic)); + Buf += strlen(Magic); + const uint64_t DecompressedSize = + support::endian::read64be(&Sec.DecompressedSize); + memcpy(Buf, &DecompressedSize, sizeof(DecompressedSize)); + Buf += sizeof(DecompressedSize); + } else { + Elf_Chdr_Impl Chdr; + Chdr.ch_type = ELF::ELFCOMPRESS_ZLIB; + Chdr.ch_size = Sec.DecompressedSize; + Chdr.ch_addralign = Sec.DecompressedAlign; + memcpy(Buf, &Chdr, sizeof(Chdr)); + Buf += sizeof(Chdr); + } + + std::copy(Sec.CompressedData.begin(), Sec.CompressedData.end(), Buf); +} + +CompressedSection::CompressedSection(const SectionBase &Sec, + DebugCompressionType CompressionType) + : SectionBase(Sec), CompressionType(CompressionType), + DecompressedSize(Sec.OriginalData.size()), DecompressedAlign(Sec.Align) { + if (Error E = zlib::compress( + StringRef(reinterpret_cast(OriginalData.data()), + OriginalData.size()), + CompressedData)) + reportError(Name, std::move(E)); + + size_t ChdrSize; + if (CompressionType == DebugCompressionType::GNU) { + Name = ".z" + Sec.Name.substr(1); + ChdrSize = sizeof("ZLIB") - 1 + sizeof(uint64_t); + } else { + Flags |= ELF::SHF_COMPRESSED; + ChdrSize = + std::max(std::max(sizeof(object::Elf_Chdr_Impl), + sizeof(object::Elf_Chdr_Impl)), + std::max(sizeof(object::Elf_Chdr_Impl), + sizeof(object::Elf_Chdr_Impl))); + } + Size = ChdrSize + CompressedData.size(); + Align = 8; +} + +CompressedSection::CompressedSection(ArrayRef CompressedData, + uint64_t DecompressedSize, + uint64_t DecompressedAlign) + : CompressionType(DebugCompressionType::None), + DecompressedSize(DecompressedSize), DecompressedAlign(DecompressedAlign) { + OriginalData = CompressedData; +} + +void CompressedSection::accept(SectionVisitor &Visitor) const { + Visitor.visit(*this); +} + +void CompressedSection::accept(MutableSectionVisitor &Visitor) { + Visitor.visit(*this); +} + +void StringTableSection::addString(StringRef Name) { StrTabBuilder.add(Name); } + +uint32_t StringTableSection::findIndex(StringRef Name) const { + return StrTabBuilder.getOffset(Name); +} + +void StringTableSection::prepareForLayout() { + StrTabBuilder.finalize(); + Size = StrTabBuilder.getSize(); +} + +void SectionWriter::visit(const StringTableSection &Sec) { + Sec.StrTabBuilder.write(Out.getBufferStart() + Sec.Offset); +} + +void StringTableSection::accept(SectionVisitor &Visitor) const { + Visitor.visit(*this); +} + +void StringTableSection::accept(MutableSectionVisitor &Visitor) { + Visitor.visit(*this); +} + +template +void ELFSectionWriter::visit(const SectionIndexSection &Sec) { + uint8_t *Buf = Out.getBufferStart() + Sec.Offset; + llvm::copy(Sec.Indexes, reinterpret_cast(Buf)); +} + +void SectionIndexSection::initialize(SectionTableRef SecTable) { + Size = 0; + setSymTab(SecTable.getSectionOfType( + Link, + "Link field value " + Twine(Link) + " in section " + Name + " is invalid", + "Link field value " + Twine(Link) + " in section " + Name + + " is not a symbol table")); + Symbols->setShndxTable(this); +} + +void SectionIndexSection::finalize() { Link = Symbols->Index; } + +void SectionIndexSection::accept(SectionVisitor &Visitor) const { + Visitor.visit(*this); +} + +void SectionIndexSection::accept(MutableSectionVisitor &Visitor) { + Visitor.visit(*this); +} + +static bool isValidReservedSectionIndex(uint16_t Index, uint16_t Machine) { + switch (Index) { + case SHN_ABS: + case SHN_COMMON: + return true; + } + + if (Machine == EM_AMDGPU) { + return Index == SHN_AMDGPU_LDS; + } + + if (Machine == EM_HEXAGON) { + switch (Index) { + case SHN_HEXAGON_SCOMMON: + case SHN_HEXAGON_SCOMMON_1: + case SHN_HEXAGON_SCOMMON_2: + case SHN_HEXAGON_SCOMMON_4: + case SHN_HEXAGON_SCOMMON_8: + return true; + } + } + return false; +} + +// Large indexes force us to clarify exactly what this function should do. This +// function should return the value that will appear in st_shndx when written +// out. +uint16_t Symbol::getShndx() const { + if (DefinedIn != nullptr) { + if (DefinedIn->Index >= SHN_LORESERVE) + return SHN_XINDEX; + return DefinedIn->Index; + } + + if (ShndxType == SYMBOL_SIMPLE_INDEX) { + // This means that we don't have a defined section but we do need to + // output a legitimate section index. + return SHN_UNDEF; + } + + assert(ShndxType == SYMBOL_ABS || ShndxType == SYMBOL_COMMON || + (ShndxType >= SYMBOL_LOPROC && ShndxType <= SYMBOL_HIPROC) || + (ShndxType >= SYMBOL_LOOS && ShndxType <= SYMBOL_HIOS)); + return static_cast(ShndxType); +} + +bool Symbol::isCommon() const { return getShndx() == SHN_COMMON; } + +void SymbolTableSection::assignIndices() { + uint32_t Index = 0; + for (auto &Sym : Symbols) + Sym->Index = Index++; +} + +void SymbolTableSection::addSymbol(Twine Name, uint8_t Bind, uint8_t Type, + SectionBase *DefinedIn, uint64_t Value, + uint8_t Visibility, uint16_t Shndx, + uint64_t SymbolSize) { + Symbol Sym; + Sym.Name = Name.str(); + Sym.Binding = Bind; + Sym.Type = Type; + Sym.DefinedIn = DefinedIn; + if (DefinedIn != nullptr) + DefinedIn->HasSymbol = true; + if (DefinedIn == nullptr) { + if (Shndx >= SHN_LORESERVE) + Sym.ShndxType = static_cast(Shndx); + else + Sym.ShndxType = SYMBOL_SIMPLE_INDEX; + } + Sym.Value = Value; + Sym.Visibility = Visibility; + Sym.Size = SymbolSize; + Sym.Index = Symbols.size(); + Symbols.emplace_back(std::make_unique(Sym)); + Size += this->EntrySize; +} + +Error SymbolTableSection::removeSectionReferences( + bool AllowBrokenLinks, function_ref ToRemove) { + if (ToRemove(SectionIndexTable)) + SectionIndexTable = nullptr; + if (ToRemove(SymbolNames)) { + if (!AllowBrokenLinks) + return createStringError( + llvm::errc::invalid_argument, + "string table '%s' cannot be removed because it is " + "referenced by the symbol table '%s'", + SymbolNames->Name.data(), this->Name.data()); + SymbolNames = nullptr; + } + return removeSymbols( + [ToRemove](const Symbol &Sym) { return ToRemove(Sym.DefinedIn); }); +} + +void SymbolTableSection::updateSymbols(function_ref Callable) { + std::for_each(std::begin(Symbols) + 1, std::end(Symbols), + [Callable](SymPtr &Sym) { Callable(*Sym); }); + std::stable_partition( + std::begin(Symbols), std::end(Symbols), + [](const SymPtr &Sym) { return Sym->Binding == STB_LOCAL; }); + assignIndices(); +} + +Error SymbolTableSection::removeSymbols( + function_ref ToRemove) { + Symbols.erase( + std::remove_if(std::begin(Symbols) + 1, std::end(Symbols), + [ToRemove](const SymPtr &Sym) { return ToRemove(*Sym); }), + std::end(Symbols)); + Size = Symbols.size() * EntrySize; + assignIndices(); + return Error::success(); +} + +void SymbolTableSection::replaceSectionReferences( + const DenseMap &FromTo) { + for (std::unique_ptr &Sym : Symbols) + if (SectionBase *To = FromTo.lookup(Sym->DefinedIn)) + Sym->DefinedIn = To; +} + +void SymbolTableSection::initialize(SectionTableRef SecTable) { + Size = 0; + setStrTab(SecTable.getSectionOfType( + Link, + "Symbol table has link index of " + Twine(Link) + + " which is not a valid index", + "Symbol table has link index of " + Twine(Link) + + " which is not a string table")); +} + +void SymbolTableSection::finalize() { + uint32_t MaxLocalIndex = 0; + for (std::unique_ptr &Sym : Symbols) { + Sym->NameIndex = + SymbolNames == nullptr ? 0 : SymbolNames->findIndex(Sym->Name); + if (Sym->Binding == STB_LOCAL) + MaxLocalIndex = std::max(MaxLocalIndex, Sym->Index); + } + // Now we need to set the Link and Info fields. + Link = SymbolNames == nullptr ? 0 : SymbolNames->Index; + Info = MaxLocalIndex + 1; +} + +void SymbolTableSection::prepareForLayout() { + // Reserve proper amount of space in section index table, so we can + // layout sections correctly. We will fill the table with correct + // indexes later in fillShdnxTable. + if (SectionIndexTable) + SectionIndexTable->reserve(Symbols.size()); + + // Add all of our strings to SymbolNames so that SymbolNames has the right + // size before layout is decided. + // If the symbol names section has been removed, don't try to add strings to + // the table. + if (SymbolNames != nullptr) + for (std::unique_ptr &Sym : Symbols) + SymbolNames->addString(Sym->Name); +} + +void SymbolTableSection::fillShndxTable() { + if (SectionIndexTable == nullptr) + return; + // Fill section index table with real section indexes. This function must + // be called after assignOffsets. + for (const std::unique_ptr &Sym : Symbols) { + if (Sym->DefinedIn != nullptr && Sym->DefinedIn->Index >= SHN_LORESERVE) + SectionIndexTable->addIndex(Sym->DefinedIn->Index); + else + SectionIndexTable->addIndex(SHN_UNDEF); + } +} + +const Symbol *SymbolTableSection::getSymbolByIndex(uint32_t Index) const { + if (Symbols.size() <= Index) + error("invalid symbol index: " + Twine(Index)); + return Symbols[Index].get(); +} + +Symbol *SymbolTableSection::getSymbolByIndex(uint32_t Index) { + return const_cast( + static_cast(this)->getSymbolByIndex(Index)); +} + +template +void ELFSectionWriter::visit(const SymbolTableSection &Sec) { + Elf_Sym *Sym = reinterpret_cast(Out.getBufferStart() + Sec.Offset); + // Loop though symbols setting each entry of the symbol table. + for (const std::unique_ptr &Symbol : Sec.Symbols) { + Sym->st_name = Symbol->NameIndex; + Sym->st_value = Symbol->Value; + Sym->st_size = Symbol->Size; + Sym->st_other = Symbol->Visibility; + Sym->setBinding(Symbol->Binding); + Sym->setType(Symbol->Type); + Sym->st_shndx = Symbol->getShndx(); + ++Sym; + } +} + +void SymbolTableSection::accept(SectionVisitor &Visitor) const { + Visitor.visit(*this); +} + +void SymbolTableSection::accept(MutableSectionVisitor &Visitor) { + Visitor.visit(*this); +} + +Error RelocationSection::removeSectionReferences( + bool AllowBrokenLinks, function_ref ToRemove) { + if (ToRemove(Symbols)) { + if (!AllowBrokenLinks) + return createStringError( + llvm::errc::invalid_argument, + "symbol table '%s' cannot be removed because it is " + "referenced by the relocation section '%s'", + Symbols->Name.data(), this->Name.data()); + Symbols = nullptr; + } + + for (const Relocation &R : Relocations) { + if (!R.RelocSymbol || !R.RelocSymbol->DefinedIn || + !ToRemove(R.RelocSymbol->DefinedIn)) + continue; + return createStringError(llvm::errc::invalid_argument, + "section '%s' cannot be removed: (%s+0x%" PRIx64 + ") has relocation against symbol '%s'", + R.RelocSymbol->DefinedIn->Name.data(), + SecToApplyRel->Name.data(), R.Offset, + R.RelocSymbol->Name.c_str()); + } + + return Error::success(); +} + +template +void RelocSectionWithSymtabBase::initialize( + SectionTableRef SecTable) { + if (Link != SHN_UNDEF) + setSymTab(SecTable.getSectionOfType( + Link, + "Link field value " + Twine(Link) + " in section " + Name + + " is invalid", + "Link field value " + Twine(Link) + " in section " + Name + + " is not a symbol table")); + + if (Info != SHN_UNDEF) + setSection(SecTable.getSection(Info, "Info field value " + Twine(Info) + + " in section " + Name + + " is invalid")); + else + setSection(nullptr); +} + +template +void RelocSectionWithSymtabBase::finalize() { + this->Link = Symbols ? Symbols->Index : 0; + + if (SecToApplyRel != nullptr) + this->Info = SecToApplyRel->Index; +} + +template +static void setAddend(Elf_Rel_Impl &Rel, uint64_t Addend) {} + +template +static void setAddend(Elf_Rel_Impl &Rela, uint64_t Addend) { + Rela.r_addend = Addend; +} + +template +static void writeRel(const RelRange &Relocations, T *Buf) { + for (const auto &Reloc : Relocations) { + Buf->r_offset = Reloc.Offset; + setAddend(*Buf, Reloc.Addend); + Buf->setSymbolAndType(Reloc.RelocSymbol ? Reloc.RelocSymbol->Index : 0, + Reloc.Type, false); + ++Buf; + } +} + +template +void ELFSectionWriter::visit(const RelocationSection &Sec) { + uint8_t *Buf = Out.getBufferStart() + Sec.Offset; + if (Sec.Type == SHT_REL) + writeRel(Sec.Relocations, reinterpret_cast(Buf)); + else + writeRel(Sec.Relocations, reinterpret_cast(Buf)); +} + +void RelocationSection::accept(SectionVisitor &Visitor) const { + Visitor.visit(*this); +} + +void RelocationSection::accept(MutableSectionVisitor &Visitor) { + Visitor.visit(*this); +} + +Error RelocationSection::removeSymbols( + function_ref ToRemove) { + for (const Relocation &Reloc : Relocations) + if (Reloc.RelocSymbol && ToRemove(*Reloc.RelocSymbol)) + return createStringError( + llvm::errc::invalid_argument, + "not stripping symbol '%s' because it is named in a relocation", + Reloc.RelocSymbol->Name.data()); + return Error::success(); +} + +void RelocationSection::markSymbols() { + for (const Relocation &Reloc : Relocations) + if (Reloc.RelocSymbol) + Reloc.RelocSymbol->Referenced = true; +} + +void RelocationSection::replaceSectionReferences( + const DenseMap &FromTo) { + // Update the target section if it was replaced. + if (SectionBase *To = FromTo.lookup(SecToApplyRel)) + SecToApplyRel = To; +} + +void SectionWriter::visit(const DynamicRelocationSection &Sec) { + llvm::copy(Sec.Contents, Out.getBufferStart() + Sec.Offset); +} + +void DynamicRelocationSection::accept(SectionVisitor &Visitor) const { + Visitor.visit(*this); +} + +void DynamicRelocationSection::accept(MutableSectionVisitor &Visitor) { + Visitor.visit(*this); +} + +Error DynamicRelocationSection::removeSectionReferences( + bool AllowBrokenLinks, function_ref ToRemove) { + if (ToRemove(Symbols)) { + if (!AllowBrokenLinks) + return createStringError( + llvm::errc::invalid_argument, + "symbol table '%s' cannot be removed because it is " + "referenced by the relocation section '%s'", + Symbols->Name.data(), this->Name.data()); + Symbols = nullptr; + } + + // SecToApplyRel contains a section referenced by sh_info field. It keeps + // a section to which the relocation section applies. When we remove any + // sections we also remove their relocation sections. Since we do that much + // earlier, this assert should never be triggered. + assert(!SecToApplyRel || !ToRemove(SecToApplyRel)); + return Error::success(); +} + +Error Section::removeSectionReferences( + bool AllowBrokenDependency, + function_ref ToRemove) { + if (ToRemove(LinkSection)) { + if (!AllowBrokenDependency) + return createStringError(llvm::errc::invalid_argument, + "section '%s' cannot be removed because it is " + "referenced by the section '%s'", + LinkSection->Name.data(), this->Name.data()); + LinkSection = nullptr; + } + return Error::success(); +} + +void GroupSection::finalize() { + this->Info = Sym ? Sym->Index : 0; + this->Link = SymTab ? SymTab->Index : 0; +} + +Error GroupSection::removeSectionReferences( + bool AllowBrokenLinks, function_ref ToRemove) { + if (ToRemove(SymTab)) { + if (!AllowBrokenLinks) + return createStringError( + llvm::errc::invalid_argument, + "section '.symtab' cannot be removed because it is " + "referenced by the group section '%s'", + this->Name.data()); + SymTab = nullptr; + Sym = nullptr; + } + llvm::erase_if(GroupMembers, ToRemove); + return Error::success(); +} + +Error GroupSection::removeSymbols(function_ref ToRemove) { + if (ToRemove(*Sym)) + return createStringError(llvm::errc::invalid_argument, + "symbol '%s' cannot be removed because it is " + "referenced by the section '%s[%d]'", + Sym->Name.data(), this->Name.data(), this->Index); + return Error::success(); +} + +void GroupSection::markSymbols() { + if (Sym) + Sym->Referenced = true; +} + +void GroupSection::replaceSectionReferences( + const DenseMap &FromTo) { + for (SectionBase *&Sec : GroupMembers) + if (SectionBase *To = FromTo.lookup(Sec)) + Sec = To; +} + +void GroupSection::onRemove() { + // As the header section of the group is removed, drop the Group flag in its + // former members. + for (SectionBase *Sec : GroupMembers) + Sec->Flags &= ~SHF_GROUP; +} + +void Section::initialize(SectionTableRef SecTable) { + if (Link == ELF::SHN_UNDEF) + return; + LinkSection = + SecTable.getSection(Link, "Link field value " + Twine(Link) + + " in section " + Name + " is invalid"); + if (LinkSection->Type == ELF::SHT_SYMTAB) + LinkSection = nullptr; +} + +void Section::finalize() { this->Link = LinkSection ? LinkSection->Index : 0; } + +void GnuDebugLinkSection::init(StringRef File) { + FileName = sys::path::filename(File); + // The format for the .gnu_debuglink starts with the file name and is + // followed by a null terminator and then the CRC32 of the file. The CRC32 + // should be 4 byte aligned. So we add the FileName size, a 1 for the null + // byte, and then finally push the size to alignment and add 4. + Size = alignTo(FileName.size() + 1, 4) + 4; + // The CRC32 will only be aligned if we align the whole section. + Align = 4; + Type = OriginalType = ELF::SHT_PROGBITS; + Name = ".gnu_debuglink"; + // For sections not found in segments, OriginalOffset is only used to + // establish the order that sections should go in. By using the maximum + // possible offset we cause this section to wind up at the end. + OriginalOffset = std::numeric_limits::max(); +} + +GnuDebugLinkSection::GnuDebugLinkSection(StringRef File, + uint32_t PrecomputedCRC) + : FileName(File), CRC32(PrecomputedCRC) { + init(File); +} + +template +void ELFSectionWriter::visit(const GnuDebugLinkSection &Sec) { + unsigned char *Buf = Out.getBufferStart() + Sec.Offset; + Elf_Word *CRC = + reinterpret_cast(Buf + Sec.Size - sizeof(Elf_Word)); + *CRC = Sec.CRC32; + llvm::copy(Sec.FileName, Buf); +} + +void GnuDebugLinkSection::accept(SectionVisitor &Visitor) const { + Visitor.visit(*this); +} + +void GnuDebugLinkSection::accept(MutableSectionVisitor &Visitor) { + Visitor.visit(*this); +} + +template +void ELFSectionWriter::visit(const GroupSection &Sec) { + ELF::Elf32_Word *Buf = + reinterpret_cast(Out.getBufferStart() + Sec.Offset); + *Buf++ = Sec.FlagWord; + for (SectionBase *S : Sec.GroupMembers) + support::endian::write32(Buf++, S->Index); +} + +void GroupSection::accept(SectionVisitor &Visitor) const { + Visitor.visit(*this); +} + +void GroupSection::accept(MutableSectionVisitor &Visitor) { + Visitor.visit(*this); +} + +// Returns true IFF a section is wholly inside the range of a segment +static bool sectionWithinSegment(const SectionBase &Sec, const Segment &Seg) { + // If a section is empty it should be treated like it has a size of 1. This is + // to clarify the case when an empty section lies on a boundary between two + // segments and ensures that the section "belongs" to the second segment and + // not the first. + uint64_t SecSize = Sec.Size ? Sec.Size : 1; + + if (Sec.Type == SHT_NOBITS) { + if (!(Sec.Flags & SHF_ALLOC)) + return false; + + bool SectionIsTLS = Sec.Flags & SHF_TLS; + bool SegmentIsTLS = Seg.Type == PT_TLS; + if (SectionIsTLS != SegmentIsTLS) + return false; + + return Seg.VAddr <= Sec.Addr && + Seg.VAddr + Seg.MemSize >= Sec.Addr + SecSize; + } + + return Seg.Offset <= Sec.OriginalOffset && + Seg.Offset + Seg.FileSize >= Sec.OriginalOffset + SecSize; +} + +// Returns true IFF a segment's original offset is inside of another segment's +// range. +static bool segmentOverlapsSegment(const Segment &Child, + const Segment &Parent) { + + return Parent.OriginalOffset <= Child.OriginalOffset && + Parent.OriginalOffset + Parent.FileSize > Child.OriginalOffset; +} + +static bool compareSegmentsByOffset(const Segment *A, const Segment *B) { + // Any segment without a parent segment should come before a segment + // that has a parent segment. + if (A->OriginalOffset < B->OriginalOffset) + return true; + if (A->OriginalOffset > B->OriginalOffset) + return false; + return A->Index < B->Index; +} + +void BasicELFBuilder::initFileHeader() { + Obj->Flags = 0x0; + Obj->Type = ET_REL; + Obj->OSABI = ELFOSABI_NONE; + Obj->ABIVersion = 0; + Obj->Entry = 0x0; + Obj->Machine = EM_NONE; + Obj->Version = 1; +} + +void BasicELFBuilder::initHeaderSegment() { Obj->ElfHdrSegment.Index = 0; } + +StringTableSection *BasicELFBuilder::addStrTab() { + auto &StrTab = Obj->addSection(); + StrTab.Name = ".strtab"; + + Obj->SectionNames = &StrTab; + return &StrTab; +} + +SymbolTableSection *BasicELFBuilder::addSymTab(StringTableSection *StrTab) { + auto &SymTab = Obj->addSection(); + + SymTab.Name = ".symtab"; + SymTab.Link = StrTab->Index; + + // The symbol table always needs a null symbol + SymTab.addSymbol("", 0, 0, nullptr, 0, 0, 0, 0); + + Obj->SymbolTable = &SymTab; + return &SymTab; +} + +void BasicELFBuilder::initSections() { + for (SectionBase &Sec : Obj->sections()) + Sec.initialize(Obj->sections()); +} + +void BinaryELFBuilder::addData(SymbolTableSection *SymTab) { + auto Data = ArrayRef( + reinterpret_cast(MemBuf->getBufferStart()), + MemBuf->getBufferSize()); + auto &DataSection = Obj->addSection
(Data); + DataSection.Name = ".data"; + DataSection.Type = ELF::SHT_PROGBITS; + DataSection.Size = Data.size(); + DataSection.Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE; + + std::string SanitizedFilename = MemBuf->getBufferIdentifier().str(); + std::replace_if(std::begin(SanitizedFilename), std::end(SanitizedFilename), + [](char C) { return !isalnum(C); }, '_'); + Twine Prefix = Twine("_binary_") + SanitizedFilename; + + SymTab->addSymbol(Prefix + "_start", STB_GLOBAL, STT_NOTYPE, &DataSection, + /*Value=*/0, NewSymbolVisibility, 0, 0); + SymTab->addSymbol(Prefix + "_end", STB_GLOBAL, STT_NOTYPE, &DataSection, + /*Value=*/DataSection.Size, NewSymbolVisibility, 0, 0); + SymTab->addSymbol(Prefix + "_size", STB_GLOBAL, STT_NOTYPE, nullptr, + /*Value=*/DataSection.Size, NewSymbolVisibility, SHN_ABS, + 0); +} + +std::unique_ptr BinaryELFBuilder::build() { + initFileHeader(); + initHeaderSegment(); + + SymbolTableSection *SymTab = addSymTab(addStrTab()); + initSections(); + addData(SymTab); + + return std::move(Obj); +} + +// Adds sections from IHEX data file. Data should have been +// fully validated by this time. +void IHexELFBuilder::addDataSections() { + OwnedDataSection *Section = nullptr; + uint64_t SegmentAddr = 0, BaseAddr = 0; + uint32_t SecNo = 1; + + for (const IHexRecord &R : Records) { + uint64_t RecAddr; + switch (R.Type) { + case IHexRecord::Data: + // Ignore empty data records + if (R.HexData.empty()) + continue; + RecAddr = R.Addr + SegmentAddr + BaseAddr; + if (!Section || Section->Addr + Section->Size != RecAddr) + // OriginalOffset field is only used to sort section properly, so + // instead of keeping track of real offset in IHEX file, we use + // section number. + Section = &Obj->addSection( + ".sec" + std::to_string(SecNo++), RecAddr, + ELF::SHF_ALLOC | ELF::SHF_WRITE, SecNo); + Section->appendHexData(R.HexData); + break; + case IHexRecord::EndOfFile: + break; + case IHexRecord::SegmentAddr: + // 20-bit segment address. + SegmentAddr = checkedGetHex(R.HexData) << 4; + break; + case IHexRecord::StartAddr80x86: + case IHexRecord::StartAddr: + Obj->Entry = checkedGetHex(R.HexData); + assert(Obj->Entry <= 0xFFFFFU); + break; + case IHexRecord::ExtendedAddr: + // 16-31 bits of linear base address + BaseAddr = checkedGetHex(R.HexData) << 16; + break; + default: + llvm_unreachable("unknown record type"); + } + } +} + +std::unique_ptr IHexELFBuilder::build() { + initFileHeader(); + initHeaderSegment(); + StringTableSection *StrTab = addStrTab(); + addSymTab(StrTab); + initSections(); + addDataSections(); + + return std::move(Obj); +} + +template void ELFBuilder::setParentSegment(Segment &Child) { + for (Segment &Parent : Obj.segments()) { + // Every segment will overlap with itself but we don't want a segment to + // be its own parent so we avoid that situation. + if (&Child != &Parent && segmentOverlapsSegment(Child, Parent)) { + // We want a canonical "most parental" segment but this requires + // inspecting the ParentSegment. + if (compareSegmentsByOffset(&Parent, &Child)) + if (Child.ParentSegment == nullptr || + compareSegmentsByOffset(&Parent, Child.ParentSegment)) { + Child.ParentSegment = &Parent; + } + } + } +} + +template void ELFBuilder::findEhdrOffset() { + if (!ExtractPartition) + return; + + for (const SectionBase &Sec : Obj.sections()) { + if (Sec.Type == SHT_LLVM_PART_EHDR && Sec.Name == *ExtractPartition) { + EhdrOffset = Sec.Offset; + return; + } + } + error("could not find partition named '" + *ExtractPartition + "'"); +} + +template +void ELFBuilder::readProgramHeaders(const ELFFile &HeadersFile) { + uint32_t Index = 0; + for (const auto &Phdr : unwrapOrError(HeadersFile.program_headers())) { + if (Phdr.p_offset + Phdr.p_filesz > HeadersFile.getBufSize()) + error("program header with offset 0x" + Twine::utohexstr(Phdr.p_offset) + + " and file size 0x" + Twine::utohexstr(Phdr.p_filesz) + + " goes past the end of the file"); + + ArrayRef Data{HeadersFile.base() + Phdr.p_offset, + (size_t)Phdr.p_filesz}; + Segment &Seg = Obj.addSegment(Data); + Seg.Type = Phdr.p_type; + Seg.Flags = Phdr.p_flags; + Seg.OriginalOffset = Phdr.p_offset + EhdrOffset; + Seg.Offset = Phdr.p_offset + EhdrOffset; + Seg.VAddr = Phdr.p_vaddr; + Seg.PAddr = Phdr.p_paddr; + Seg.FileSize = Phdr.p_filesz; + Seg.MemSize = Phdr.p_memsz; + Seg.Align = Phdr.p_align; + Seg.Index = Index++; + for (SectionBase &Sec : Obj.sections()) + if (sectionWithinSegment(Sec, Seg)) { + Seg.addSection(&Sec); + if (!Sec.ParentSegment || Sec.ParentSegment->Offset > Seg.Offset) + Sec.ParentSegment = &Seg; + } + } + + auto &ElfHdr = Obj.ElfHdrSegment; + ElfHdr.Index = Index++; + ElfHdr.OriginalOffset = ElfHdr.Offset = EhdrOffset; + + const auto &Ehdr = *HeadersFile.getHeader(); + auto &PrHdr = Obj.ProgramHdrSegment; + PrHdr.Type = PT_PHDR; + PrHdr.Flags = 0; + // The spec requires us to have p_vaddr % p_align == p_offset % p_align. + // Whereas this works automatically for ElfHdr, here OriginalOffset is + // always non-zero and to ensure the equation we assign the same value to + // VAddr as well. + PrHdr.OriginalOffset = PrHdr.Offset = PrHdr.VAddr = EhdrOffset + Ehdr.e_phoff; + PrHdr.PAddr = 0; + PrHdr.FileSize = PrHdr.MemSize = Ehdr.e_phentsize * Ehdr.e_phnum; + // The spec requires us to naturally align all the fields. + PrHdr.Align = sizeof(Elf_Addr); + PrHdr.Index = Index++; + + // Now we do an O(n^2) loop through the segments in order to match up + // segments. + for (Segment &Child : Obj.segments()) + setParentSegment(Child); + setParentSegment(ElfHdr); + setParentSegment(PrHdr); +} + +template +void ELFBuilder::initGroupSection(GroupSection *GroupSec) { + if (GroupSec->Align % sizeof(ELF::Elf32_Word) != 0) + error("invalid alignment " + Twine(GroupSec->Align) + + " of group section '" + GroupSec->Name + "'"); + SectionTableRef SecTable = Obj.sections(); + if (GroupSec->Link != SHN_UNDEF) { + auto SymTab = SecTable.template getSectionOfType( + GroupSec->Link, + "link field value '" + Twine(GroupSec->Link) + "' in section '" + + GroupSec->Name + "' is invalid", + "link field value '" + Twine(GroupSec->Link) + "' in section '" + + GroupSec->Name + "' is not a symbol table"); + Symbol *Sym = SymTab->getSymbolByIndex(GroupSec->Info); + if (!Sym) + error("info field value '" + Twine(GroupSec->Info) + "' in section '" + + GroupSec->Name + "' is not a valid symbol index"); + GroupSec->setSymTab(SymTab); + GroupSec->setSymbol(Sym); + } + if (GroupSec->Contents.size() % sizeof(ELF::Elf32_Word) || + GroupSec->Contents.empty()) + error("the content of the section " + GroupSec->Name + " is malformed"); + const ELF::Elf32_Word *Word = + reinterpret_cast(GroupSec->Contents.data()); + const ELF::Elf32_Word *End = + Word + GroupSec->Contents.size() / sizeof(ELF::Elf32_Word); + GroupSec->setFlagWord(*Word++); + for (; Word != End; ++Word) { + uint32_t Index = support::endian::read32(Word); + GroupSec->addMember(SecTable.getSection( + Index, "group member index " + Twine(Index) + " in section '" + + GroupSec->Name + "' is invalid")); + } +} + +template +void ELFBuilder::initSymbolTable(SymbolTableSection *SymTab) { + const Elf_Shdr &Shdr = *unwrapOrError(ElfFile.getSection(SymTab->Index)); + StringRef StrTabData = unwrapOrError(ElfFile.getStringTableForSymtab(Shdr)); + ArrayRef ShndxData; + + auto Symbols = unwrapOrError(ElfFile.symbols(&Shdr)); + for (const auto &Sym : Symbols) { + SectionBase *DefSection = nullptr; + StringRef Name = unwrapOrError(Sym.getName(StrTabData)); + + if (Sym.st_shndx == SHN_XINDEX) { + if (SymTab->getShndxTable() == nullptr) + error("symbol '" + Name + + "' has index SHN_XINDEX but no SHT_SYMTAB_SHNDX section exists"); + if (ShndxData.data() == nullptr) { + const Elf_Shdr &ShndxSec = + *unwrapOrError(ElfFile.getSection(SymTab->getShndxTable()->Index)); + ShndxData = unwrapOrError( + ElfFile.template getSectionContentsAsArray(&ShndxSec)); + if (ShndxData.size() != Symbols.size()) + error("symbol section index table does not have the same number of " + "entries as the symbol table"); + } + Elf_Word Index = ShndxData[&Sym - Symbols.begin()]; + DefSection = Obj.sections().getSection( + Index, + "symbol '" + Name + "' has invalid section index " + Twine(Index)); + } else if (Sym.st_shndx >= SHN_LORESERVE) { + if (!isValidReservedSectionIndex(Sym.st_shndx, Obj.Machine)) { + error( + "symbol '" + Name + + "' has unsupported value greater than or equal to SHN_LORESERVE: " + + Twine(Sym.st_shndx)); + } + } else if (Sym.st_shndx != SHN_UNDEF) { + DefSection = Obj.sections().getSection( + Sym.st_shndx, "symbol '" + Name + + "' is defined has invalid section index " + + Twine(Sym.st_shndx)); + } + + SymTab->addSymbol(Name, Sym.getBinding(), Sym.getType(), DefSection, + Sym.getValue(), Sym.st_other, Sym.st_shndx, Sym.st_size); + } +} + +template +static void getAddend(uint64_t &ToSet, const Elf_Rel_Impl &Rel) {} + +template +static void getAddend(uint64_t &ToSet, const Elf_Rel_Impl &Rela) { + ToSet = Rela.r_addend; +} + +template +static void initRelocations(RelocationSection *Relocs, + SymbolTableSection *SymbolTable, T RelRange) { + for (const auto &Rel : RelRange) { + Relocation ToAdd; + ToAdd.Offset = Rel.r_offset; + getAddend(ToAdd.Addend, Rel); + ToAdd.Type = Rel.getType(false); + + if (uint32_t Sym = Rel.getSymbol(false)) { + if (!SymbolTable) + error("'" + Relocs->Name + + "': relocation references symbol with index " + Twine(Sym) + + ", but there is no symbol table"); + ToAdd.RelocSymbol = SymbolTable->getSymbolByIndex(Sym); + } + + Relocs->addRelocation(ToAdd); + } +} + +SectionBase *SectionTableRef::getSection(uint32_t Index, Twine ErrMsg) { + if (Index == SHN_UNDEF || Index > Sections.size()) + error(ErrMsg); + return Sections[Index - 1].get(); +} + +template +T *SectionTableRef::getSectionOfType(uint32_t Index, Twine IndexErrMsg, + Twine TypeErrMsg) { + if (T *Sec = dyn_cast(getSection(Index, IndexErrMsg))) + return Sec; + error(TypeErrMsg); +} + +template +SectionBase &ELFBuilder::makeSection(const Elf_Shdr &Shdr) { + ArrayRef Data; + switch (Shdr.sh_type) { + case SHT_REL: + case SHT_RELA: + if (Shdr.sh_flags & SHF_ALLOC) { + Data = unwrapOrError(ElfFile.getSectionContents(&Shdr)); + return Obj.addSection(Data); + } + return Obj.addSection(); + case SHT_STRTAB: + // If a string table is allocated we don't want to mess with it. That would + // mean altering the memory image. There are no special link types or + // anything so we can just use a Section. + if (Shdr.sh_flags & SHF_ALLOC) { + Data = unwrapOrError(ElfFile.getSectionContents(&Shdr)); + return Obj.addSection
(Data); + } + return Obj.addSection(); + case SHT_HASH: + case SHT_GNU_HASH: + // Hash tables should refer to SHT_DYNSYM which we're not going to change. + // Because of this we don't need to mess with the hash tables either. + Data = unwrapOrError(ElfFile.getSectionContents(&Shdr)); + return Obj.addSection
(Data); + case SHT_GROUP: + Data = unwrapOrError(ElfFile.getSectionContents(&Shdr)); + return Obj.addSection(Data); + case SHT_DYNSYM: + Data = unwrapOrError(ElfFile.getSectionContents(&Shdr)); + return Obj.addSection(Data); + case SHT_DYNAMIC: + Data = unwrapOrError(ElfFile.getSectionContents(&Shdr)); + return Obj.addSection(Data); + case SHT_SYMTAB: { + auto &SymTab = Obj.addSection(); + Obj.SymbolTable = &SymTab; + return SymTab; + } + case SHT_SYMTAB_SHNDX: { + auto &ShndxSection = Obj.addSection(); + Obj.SectionIndexTable = &ShndxSection; + return ShndxSection; + } + case SHT_NOBITS: + return Obj.addSection
(Data); + default: { + Data = unwrapOrError(ElfFile.getSectionContents(&Shdr)); + + StringRef Name = unwrapOrError(ElfFile.getSectionName(&Shdr)); + if (Name.startswith(".zdebug") || (Shdr.sh_flags & ELF::SHF_COMPRESSED)) { + uint64_t DecompressedSize, DecompressedAlign; + std::tie(DecompressedSize, DecompressedAlign) = + getDecompressedSizeAndAlignment(Data); + return Obj.addSection(Data, DecompressedSize, + DecompressedAlign); + } + + return Obj.addSection
(Data); + } + } +} + +template void ELFBuilder::readSectionHeaders() { + uint32_t Index = 0; + for (const auto &Shdr : unwrapOrError(ElfFile.sections())) { + if (Index == 0) { + ++Index; + continue; + } + auto &Sec = makeSection(Shdr); + Sec.Name = std::string(unwrapOrError(ElfFile.getSectionName(&Shdr))); + Sec.Type = Sec.OriginalType = Shdr.sh_type; + Sec.Flags = Sec.OriginalFlags = Shdr.sh_flags; + Sec.Addr = Shdr.sh_addr; + Sec.Offset = Shdr.sh_offset; + Sec.OriginalOffset = Shdr.sh_offset; + Sec.Size = Shdr.sh_size; + Sec.Link = Shdr.sh_link; + Sec.Info = Shdr.sh_info; + Sec.Align = Shdr.sh_addralign; + Sec.EntrySize = Shdr.sh_entsize; + Sec.Index = Index++; + Sec.OriginalData = + ArrayRef(ElfFile.base() + Shdr.sh_offset, + (Shdr.sh_type == SHT_NOBITS) ? 0 : Shdr.sh_size); + } +} + +template void ELFBuilder::readSections(bool EnsureSymtab) { + uint32_t ShstrIndex = ElfFile.getHeader()->e_shstrndx; + if (ShstrIndex == SHN_XINDEX) + ShstrIndex = unwrapOrError(ElfFile.getSection(0))->sh_link; + + if (ShstrIndex == SHN_UNDEF) + Obj.HadShdrs = false; + else + Obj.SectionNames = + Obj.sections().template getSectionOfType( + ShstrIndex, + "e_shstrndx field value " + Twine(ShstrIndex) + " in elf header " + + " is invalid", + "e_shstrndx field value " + Twine(ShstrIndex) + " in elf header " + + " does not reference a string table"); + + // If a section index table exists we'll need to initialize it before we + // initialize the symbol table because the symbol table might need to + // reference it. + if (Obj.SectionIndexTable) + Obj.SectionIndexTable->initialize(Obj.sections()); + + // Now that all of the sections have been added we can fill out some extra + // details about symbol tables. We need the symbol table filled out before + // any relocations. + if (Obj.SymbolTable) { + Obj.SymbolTable->initialize(Obj.sections()); + initSymbolTable(Obj.SymbolTable); + } else if (EnsureSymtab) { + Obj.addNewSymbolTable(); + } + + // Now that all sections and symbols have been added we can add + // relocations that reference symbols and set the link and info fields for + // relocation sections. + for (auto &Sec : Obj.sections()) { + if (&Sec == Obj.SymbolTable) + continue; + Sec.initialize(Obj.sections()); + if (auto RelSec = dyn_cast(&Sec)) { + auto Shdr = unwrapOrError(ElfFile.sections()).begin() + RelSec->Index; + if (RelSec->Type == SHT_REL) + initRelocations(RelSec, Obj.SymbolTable, + unwrapOrError(ElfFile.rels(Shdr))); + else + initRelocations(RelSec, Obj.SymbolTable, + unwrapOrError(ElfFile.relas(Shdr))); + } else if (auto GroupSec = dyn_cast(&Sec)) { + initGroupSection(GroupSec); + } + } +} + +template void ELFBuilder::build(bool EnsureSymtab) { + readSectionHeaders(); + findEhdrOffset(); + + // The ELFFile whose ELF headers and program headers are copied into the + // output file. Normally the same as ElfFile, but if we're extracting a + // loadable partition it will point to the partition's headers. + ELFFile HeadersFile = unwrapOrError(ELFFile::create(toStringRef( + {ElfFile.base() + EhdrOffset, ElfFile.getBufSize() - EhdrOffset}))); + + auto &Ehdr = *HeadersFile.getHeader(); + Obj.OSABI = Ehdr.e_ident[EI_OSABI]; + Obj.ABIVersion = Ehdr.e_ident[EI_ABIVERSION]; + Obj.Type = Ehdr.e_type; + Obj.Machine = Ehdr.e_machine; + Obj.Version = Ehdr.e_version; + Obj.Entry = Ehdr.e_entry; + Obj.Flags = Ehdr.e_flags; + + readSections(EnsureSymtab); + readProgramHeaders(HeadersFile); +} + +Writer::~Writer() {} + +Reader::~Reader() {} + +std::unique_ptr BinaryReader::create(bool /*EnsureSymtab*/) const { + return BinaryELFBuilder(MemBuf, NewSymbolVisibility).build(); +} + +Expected> IHexReader::parse() const { + SmallVector Lines; + std::vector Records; + bool HasSections = false; + + MemBuf->getBuffer().split(Lines, '\n'); + Records.reserve(Lines.size()); + for (size_t LineNo = 1; LineNo <= Lines.size(); ++LineNo) { + StringRef Line = Lines[LineNo - 1].trim(); + if (Line.empty()) + continue; + + Expected R = IHexRecord::parse(Line); + if (!R) + return parseError(LineNo, R.takeError()); + if (R->Type == IHexRecord::EndOfFile) + break; + HasSections |= (R->Type == IHexRecord::Data); + Records.push_back(*R); + } + if (!HasSections) + return parseError(-1U, "no sections"); + + return std::move(Records); +} + +std::unique_ptr IHexReader::create(bool /*EnsureSymtab*/) const { + std::vector Records = unwrapOrError(parse()); + return IHexELFBuilder(Records).build(); +} + +std::unique_ptr ELFReader::create(bool EnsureSymtab) const { + auto Obj = std::make_unique(); + if (auto *O = dyn_cast>(Bin)) { + ELFBuilder Builder(*O, *Obj, ExtractPartition); + Builder.build(EnsureSymtab); + return Obj; + } else if (auto *O = dyn_cast>(Bin)) { + ELFBuilder Builder(*O, *Obj, ExtractPartition); + Builder.build(EnsureSymtab); + return Obj; + } else if (auto *O = dyn_cast>(Bin)) { + ELFBuilder Builder(*O, *Obj, ExtractPartition); + Builder.build(EnsureSymtab); + return Obj; + } else if (auto *O = dyn_cast>(Bin)) { + ELFBuilder Builder(*O, *Obj, ExtractPartition); + Builder.build(EnsureSymtab); + return Obj; + } + error("invalid file type"); +} + +template void ELFWriter::writeEhdr() { + Elf_Ehdr &Ehdr = *reinterpret_cast(Buf.getBufferStart()); + std::fill(Ehdr.e_ident, Ehdr.e_ident + 16, 0); + Ehdr.e_ident[EI_MAG0] = 0x7f; + Ehdr.e_ident[EI_MAG1] = 'E'; + Ehdr.e_ident[EI_MAG2] = 'L'; + Ehdr.e_ident[EI_MAG3] = 'F'; + Ehdr.e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32; + Ehdr.e_ident[EI_DATA] = + ELFT::TargetEndianness == support::big ? ELFDATA2MSB : ELFDATA2LSB; + Ehdr.e_ident[EI_VERSION] = EV_CURRENT; + Ehdr.e_ident[EI_OSABI] = Obj.OSABI; + Ehdr.e_ident[EI_ABIVERSION] = Obj.ABIVersion; + + Ehdr.e_type = Obj.Type; + Ehdr.e_machine = Obj.Machine; + Ehdr.e_version = Obj.Version; + Ehdr.e_entry = Obj.Entry; + // We have to use the fully-qualified name llvm::size + // since some compilers complain on ambiguous resolution. + Ehdr.e_phnum = llvm::size(Obj.segments()); + Ehdr.e_phoff = (Ehdr.e_phnum != 0) ? Obj.ProgramHdrSegment.Offset : 0; + Ehdr.e_phentsize = (Ehdr.e_phnum != 0) ? sizeof(Elf_Phdr) : 0; + Ehdr.e_flags = Obj.Flags; + Ehdr.e_ehsize = sizeof(Elf_Ehdr); + if (WriteSectionHeaders && Obj.sections().size() != 0) { + Ehdr.e_shentsize = sizeof(Elf_Shdr); + Ehdr.e_shoff = Obj.SHOff; + // """ + // If the number of sections is greater than or equal to + // SHN_LORESERVE (0xff00), this member has the value zero and the actual + // number of section header table entries is contained in the sh_size field + // of the section header at index 0. + // """ + auto Shnum = Obj.sections().size() + 1; + if (Shnum >= SHN_LORESERVE) + Ehdr.e_shnum = 0; + else + Ehdr.e_shnum = Shnum; + // """ + // If the section name string table section index is greater than or equal + // to SHN_LORESERVE (0xff00), this member has the value SHN_XINDEX (0xffff) + // and the actual index of the section name string table section is + // contained in the sh_link field of the section header at index 0. + // """ + if (Obj.SectionNames->Index >= SHN_LORESERVE) + Ehdr.e_shstrndx = SHN_XINDEX; + else + Ehdr.e_shstrndx = Obj.SectionNames->Index; + } else { + Ehdr.e_shentsize = 0; + Ehdr.e_shoff = 0; + Ehdr.e_shnum = 0; + Ehdr.e_shstrndx = 0; + } +} + +template void ELFWriter::writePhdrs() { + for (auto &Seg : Obj.segments()) + writePhdr(Seg); +} + +template void ELFWriter::writeShdrs() { + // This reference serves to write the dummy section header at the begining + // of the file. It is not used for anything else + Elf_Shdr &Shdr = + *reinterpret_cast(Buf.getBufferStart() + Obj.SHOff); + Shdr.sh_name = 0; + Shdr.sh_type = SHT_NULL; + Shdr.sh_flags = 0; + Shdr.sh_addr = 0; + Shdr.sh_offset = 0; + // See writeEhdr for why we do this. + uint64_t Shnum = Obj.sections().size() + 1; + if (Shnum >= SHN_LORESERVE) + Shdr.sh_size = Shnum; + else + Shdr.sh_size = 0; + // See writeEhdr for why we do this. + if (Obj.SectionNames != nullptr && Obj.SectionNames->Index >= SHN_LORESERVE) + Shdr.sh_link = Obj.SectionNames->Index; + else + Shdr.sh_link = 0; + Shdr.sh_info = 0; + Shdr.sh_addralign = 0; + Shdr.sh_entsize = 0; + + for (SectionBase &Sec : Obj.sections()) + writeShdr(Sec); +} + +template void ELFWriter::writeSectionData() { + for (SectionBase &Sec : Obj.sections()) + // Segments are responsible for writing their contents, so only write the + // section data if the section is not in a segment. Note that this renders + // sections in segments effectively immutable. + if (Sec.ParentSegment == nullptr) + Sec.accept(*SecWriter); +} + +template void ELFWriter::writeSegmentData() { + for (Segment &Seg : Obj.segments()) { + size_t Size = std::min(Seg.FileSize, Seg.getContents().size()); + std::memcpy(Buf.getBufferStart() + Seg.Offset, Seg.getContents().data(), + Size); + } + + // Iterate over removed sections and overwrite their old data with zeroes. + for (auto &Sec : Obj.removedSections()) { + Segment *Parent = Sec.ParentSegment; + if (Parent == nullptr || Sec.Type == SHT_NOBITS || Sec.Size == 0) + continue; + uint64_t Offset = + Sec.OriginalOffset - Parent->OriginalOffset + Parent->Offset; + std::memset(Buf.getBufferStart() + Offset, 0, Sec.Size); + } +} + +template +ELFWriter::ELFWriter(Object &Obj, Buffer &Buf, bool WSH, + bool OnlyKeepDebug) + : Writer(Obj, Buf), WriteSectionHeaders(WSH && Obj.HadShdrs), + OnlyKeepDebug(OnlyKeepDebug) {} + +Error Object::removeSections( + bool AllowBrokenLinks, std::function ToRemove) { + + auto Iter = std::stable_partition( + std::begin(Sections), std::end(Sections), [=](const SecPtr &Sec) { + if (ToRemove(*Sec)) + return false; + if (auto RelSec = dyn_cast(Sec.get())) { + if (auto ToRelSec = RelSec->getSection()) + return !ToRemove(*ToRelSec); + } + return true; + }); + if (SymbolTable != nullptr && ToRemove(*SymbolTable)) + SymbolTable = nullptr; + if (SectionNames != nullptr && ToRemove(*SectionNames)) + SectionNames = nullptr; + if (SectionIndexTable != nullptr && ToRemove(*SectionIndexTable)) + SectionIndexTable = nullptr; + // Now make sure there are no remaining references to the sections that will + // be removed. Sometimes it is impossible to remove a reference so we emit + // an error here instead. + std::unordered_set RemoveSections; + RemoveSections.reserve(std::distance(Iter, std::end(Sections))); + for (auto &RemoveSec : make_range(Iter, std::end(Sections))) { + for (auto &Segment : Segments) + Segment->removeSection(RemoveSec.get()); + RemoveSec->onRemove(); + RemoveSections.insert(RemoveSec.get()); + } + + // For each section that remains alive, we want to remove the dead references. + // This either might update the content of the section (e.g. remove symbols + // from symbol table that belongs to removed section) or trigger an error if + // a live section critically depends on a section being removed somehow + // (e.g. the removed section is referenced by a relocation). + for (auto &KeepSec : make_range(std::begin(Sections), Iter)) { + if (Error E = KeepSec->removeSectionReferences( + AllowBrokenLinks, [&RemoveSections](const SectionBase *Sec) { + return RemoveSections.find(Sec) != RemoveSections.end(); + })) + return E; + } + + // Transfer removed sections into the Object RemovedSections container for use + // later. + std::move(Iter, Sections.end(), std::back_inserter(RemovedSections)); + // Now finally get rid of them all together. + Sections.erase(Iter, std::end(Sections)); + return Error::success(); +} + +Error Object::removeSymbols(function_ref ToRemove) { + if (SymbolTable) + for (const SecPtr &Sec : Sections) + if (Error E = Sec->removeSymbols(ToRemove)) + return E; + return Error::success(); +} + +void Object::addNewSymbolTable() { + assert(!SymbolTable && "Object must not has a SymbolTable."); + + // Reuse an existing SHT_STRTAB section if it exists. + StringTableSection *StrTab = nullptr; + for (SectionBase &Sec : sections()) { + if (Sec.Type == ELF::SHT_STRTAB && !(Sec.Flags & SHF_ALLOC)) { + StrTab = static_cast(&Sec); + + // Prefer a string table that is not the section header string table, if + // such a table exists. + if (SectionNames != &Sec) + break; + } + } + if (!StrTab) + StrTab = &addSection(); + + SymbolTableSection &SymTab = addSection(); + SymTab.Name = ".symtab"; + SymTab.Link = StrTab->Index; + SymTab.initialize(sections()); + SymTab.addSymbol("", 0, 0, nullptr, 0, 0, 0, 0); + + SymbolTable = &SymTab; +} + +void Object::sortSections() { + // Use stable_sort to maintain the original ordering as closely as possible. + llvm::stable_sort(Sections, [](const SecPtr &A, const SecPtr &B) { + // Put SHT_GROUP sections first, since group section headers must come + // before the sections they contain. This also matches what GNU objcopy + // does. + if (A->Type != B->Type && + (A->Type == ELF::SHT_GROUP || B->Type == ELF::SHT_GROUP)) + return A->Type == ELF::SHT_GROUP; + // For all other sections, sort by offset order. + return A->OriginalOffset < B->OriginalOffset; + }); +} + +// Orders segments such that if x = y->ParentSegment then y comes before x. +static void orderSegments(std::vector &Segments) { + llvm::stable_sort(Segments, compareSegmentsByOffset); +} + +// This function finds a consistent layout for a list of segments starting from +// an Offset. It assumes that Segments have been sorted by orderSegments and +// returns an Offset one past the end of the last segment. +static uint64_t layoutSegments(std::vector &Segments, + uint64_t Offset) { + assert(llvm::is_sorted(Segments, compareSegmentsByOffset)); + // The only way a segment should move is if a section was between two + // segments and that section was removed. If that section isn't in a segment + // then it's acceptable, but not ideal, to simply move it to after the + // segments. So we can simply layout segments one after the other accounting + // for alignment. + for (Segment *Seg : Segments) { + // We assume that segments have been ordered by OriginalOffset and Index + // such that a parent segment will always come before a child segment in + // OrderedSegments. This means that the Offset of the ParentSegment should + // already be set and we can set our offset relative to it. + if (Seg->ParentSegment != nullptr) { + Segment *Parent = Seg->ParentSegment; + Seg->Offset = + Parent->Offset + Seg->OriginalOffset - Parent->OriginalOffset; + } else { + Seg->Offset = + alignTo(Offset, std::max(Seg->Align, 1), Seg->VAddr); + } + Offset = std::max(Offset, Seg->Offset + Seg->FileSize); + } + return Offset; +} + +// This function finds a consistent layout for a list of sections. It assumes +// that the ->ParentSegment of each section has already been laid out. The +// supplied starting Offset is used for the starting offset of any section that +// does not have a ParentSegment. It returns either the offset given if all +// sections had a ParentSegment or an offset one past the last section if there +// was a section that didn't have a ParentSegment. +template +static uint64_t layoutSections(Range Sections, uint64_t Offset) { + // Now the offset of every segment has been set we can assign the offsets + // of each section. For sections that are covered by a segment we should use + // the segment's original offset and the section's original offset to compute + // the offset from the start of the segment. Using the offset from the start + // of the segment we can assign a new offset to the section. For sections not + // covered by segments we can just bump Offset to the next valid location. + uint32_t Index = 1; + for (auto &Sec : Sections) { + Sec.Index = Index++; + if (Sec.ParentSegment != nullptr) { + auto Segment = *Sec.ParentSegment; + Sec.Offset = + Segment.Offset + (Sec.OriginalOffset - Segment.OriginalOffset); + } else { + Offset = alignTo(Offset, Sec.Align == 0 ? 1 : Sec.Align); + Sec.Offset = Offset; + if (Sec.Type != SHT_NOBITS) + Offset += Sec.Size; + } + } + return Offset; +} + +// Rewrite sh_offset after some sections are changed to SHT_NOBITS and thus +// occupy no space in the file. +static uint64_t layoutSectionsForOnlyKeepDebug(Object &Obj, uint64_t Off) { + uint32_t Index = 1; + for (auto &Sec : Obj.sections()) { + Sec.Index = Index++; + + auto *FirstSec = Sec.ParentSegment && Sec.ParentSegment->Type == PT_LOAD + ? Sec.ParentSegment->firstSection() + : nullptr; + + // The first section in a PT_LOAD has to have congruent offset and address + // modulo the alignment, which usually equals the maximum page size. + if (FirstSec && FirstSec == &Sec) + Off = alignTo(Off, Sec.ParentSegment->Align, Sec.Addr); + + // sh_offset is not significant for SHT_NOBITS sections, but the congruence + // rule must be followed if it is the first section in a PT_LOAD. Do not + // advance Off. + if (Sec.Type == SHT_NOBITS) { + Sec.Offset = Off; + continue; + } + + if (!FirstSec) { + // FirstSec being nullptr generally means that Sec does not have the + // SHF_ALLOC flag. + Off = Sec.Align ? alignTo(Off, Sec.Align) : Off; + } else if (FirstSec != &Sec) { + // The offset is relative to the first section in the PT_LOAD segment. Use + // sh_offset for non-SHF_ALLOC sections. + Off = Sec.OriginalOffset - FirstSec->OriginalOffset + FirstSec->Offset; + } + Sec.Offset = Off; + Off += Sec.Size; + } + return Off; +} + +// Rewrite p_offset and p_filesz of non-empty non-PT_PHDR segments after +// sh_offset values have been updated. +static uint64_t layoutSegmentsForOnlyKeepDebug(std::vector &Segments, + uint64_t HdrEnd) { + uint64_t MaxOffset = 0; + for (Segment *Seg : Segments) { + const SectionBase *FirstSec = Seg->firstSection(); + if (Seg->Type == PT_PHDR || !FirstSec) + continue; + + uint64_t Offset = FirstSec->Offset; + uint64_t FileSize = 0; + for (const SectionBase *Sec : Seg->Sections) { + uint64_t Size = Sec->Type == SHT_NOBITS ? 0 : Sec->Size; + if (Sec->Offset + Size > Offset) + FileSize = std::max(FileSize, Sec->Offset + Size - Offset); + } + + // If the segment includes EHDR and program headers, don't make it smaller + // than the headers. + if (Seg->Offset < HdrEnd && HdrEnd <= Seg->Offset + Seg->FileSize) { + FileSize += Offset - Seg->Offset; + Offset = Seg->Offset; + FileSize = std::max(FileSize, HdrEnd - Offset); + } + + Seg->Offset = Offset; + Seg->FileSize = FileSize; + MaxOffset = std::max(MaxOffset, Offset + FileSize); + } + return MaxOffset; +} + +template void ELFWriter::initEhdrSegment() { + Segment &ElfHdr = Obj.ElfHdrSegment; + ElfHdr.Type = PT_PHDR; + ElfHdr.Flags = 0; + ElfHdr.VAddr = 0; + ElfHdr.PAddr = 0; + ElfHdr.FileSize = ElfHdr.MemSize = sizeof(Elf_Ehdr); + ElfHdr.Align = 0; +} + +template void ELFWriter::assignOffsets() { + // We need a temporary list of segments that has a special order to it + // so that we know that anytime ->ParentSegment is set that segment has + // already had its offset properly set. + std::vector OrderedSegments; + for (Segment &Segment : Obj.segments()) + OrderedSegments.push_back(&Segment); + OrderedSegments.push_back(&Obj.ElfHdrSegment); + OrderedSegments.push_back(&Obj.ProgramHdrSegment); + orderSegments(OrderedSegments); + + uint64_t Offset; + if (OnlyKeepDebug) { + // For --only-keep-debug, the sections that did not preserve contents were + // changed to SHT_NOBITS. We now rewrite sh_offset fields of sections, and + // then rewrite p_offset/p_filesz of program headers. + uint64_t HdrEnd = + sizeof(Elf_Ehdr) + llvm::size(Obj.segments()) * sizeof(Elf_Phdr); + Offset = layoutSectionsForOnlyKeepDebug(Obj, HdrEnd); + Offset = std::max(Offset, + layoutSegmentsForOnlyKeepDebug(OrderedSegments, HdrEnd)); + } else { + // Offset is used as the start offset of the first segment to be laid out. + // Since the ELF Header (ElfHdrSegment) must be at the start of the file, + // we start at offset 0. + Offset = layoutSegments(OrderedSegments, 0); + Offset = layoutSections(Obj.sections(), Offset); + } + // If we need to write the section header table out then we need to align the + // Offset so that SHOffset is valid. + if (WriteSectionHeaders) + Offset = alignTo(Offset, sizeof(Elf_Addr)); + Obj.SHOff = Offset; +} + +template size_t ELFWriter::totalSize() const { + // We already have the section header offset so we can calculate the total + // size by just adding up the size of each section header. + if (!WriteSectionHeaders) + return Obj.SHOff; + size_t ShdrCount = Obj.sections().size() + 1; // Includes null shdr. + return Obj.SHOff + ShdrCount * sizeof(Elf_Shdr); +} + +template Error ELFWriter::write() { + // Segment data must be written first, so that the ELF header and program + // header tables can overwrite it, if covered by a segment. + writeSegmentData(); + writeEhdr(); + writePhdrs(); + writeSectionData(); + if (WriteSectionHeaders) + writeShdrs(); + return Buf.commit(); +} + +static Error removeUnneededSections(Object &Obj) { + // We can remove an empty symbol table from non-relocatable objects. + // Relocatable objects typically have relocation sections whose + // sh_link field points to .symtab, so we can't remove .symtab + // even if it is empty. + if (Obj.isRelocatable() || Obj.SymbolTable == nullptr || + !Obj.SymbolTable->empty()) + return Error::success(); + + // .strtab can be used for section names. In such a case we shouldn't + // remove it. + auto *StrTab = Obj.SymbolTable->getStrTab() == Obj.SectionNames + ? nullptr + : Obj.SymbolTable->getStrTab(); + return Obj.removeSections(false, [&](const SectionBase &Sec) { + return &Sec == Obj.SymbolTable || &Sec == StrTab; + }); +} + +template Error ELFWriter::finalize() { + // It could happen that SectionNames has been removed and yet the user wants + // a section header table output. We need to throw an error if a user tries + // to do that. + if (Obj.SectionNames == nullptr && WriteSectionHeaders) + return createStringError(llvm::errc::invalid_argument, + "cannot write section header table because " + "section header string table was removed"); + + if (Error E = removeUnneededSections(Obj)) + return E; + Obj.sortSections(); + + // We need to assign indexes before we perform layout because we need to know + // if we need large indexes or not. We can assign indexes first and check as + // we go to see if we will actully need large indexes. + bool NeedsLargeIndexes = false; + if (Obj.sections().size() >= SHN_LORESERVE) { + SectionTableRef Sections = Obj.sections(); + NeedsLargeIndexes = + std::any_of(Sections.begin() + SHN_LORESERVE, Sections.end(), + [](const SectionBase &Sec) { return Sec.HasSymbol; }); + // TODO: handle case where only one section needs the large index table but + // only needs it because the large index table hasn't been removed yet. + } + + if (NeedsLargeIndexes) { + // This means we definitely need to have a section index table but if we + // already have one then we should use it instead of making a new one. + if (Obj.SymbolTable != nullptr && Obj.SectionIndexTable == nullptr) { + // Addition of a section to the end does not invalidate the indexes of + // other sections and assigns the correct index to the new section. + auto &Shndx = Obj.addSection(); + Obj.SymbolTable->setShndxTable(&Shndx); + Shndx.setSymTab(Obj.SymbolTable); + } + } else { + // Since we don't need SectionIndexTable we should remove it and all + // references to it. + if (Obj.SectionIndexTable != nullptr) { + // We do not support sections referring to the section index table. + if (Error E = Obj.removeSections(false /*AllowBrokenLinks*/, + [this](const SectionBase &Sec) { + return &Sec == Obj.SectionIndexTable; + })) + return E; + } + } + + // Make sure we add the names of all the sections. Importantly this must be + // done after we decide to add or remove SectionIndexes. + if (Obj.SectionNames != nullptr) + for (const SectionBase &Sec : Obj.sections()) + Obj.SectionNames->addString(Sec.Name); + + initEhdrSegment(); + + // Before we can prepare for layout the indexes need to be finalized. + // Also, the output arch may not be the same as the input arch, so fix up + // size-related fields before doing layout calculations. + uint64_t Index = 0; + auto SecSizer = std::make_unique>(); + for (SectionBase &Sec : Obj.sections()) { + Sec.Index = Index++; + Sec.accept(*SecSizer); + } + + // The symbol table does not update all other sections on update. For + // instance, symbol names are not added as new symbols are added. This means + // that some sections, like .strtab, don't yet have their final size. + if (Obj.SymbolTable != nullptr) + Obj.SymbolTable->prepareForLayout(); + + // Now that all strings are added we want to finalize string table builders, + // because that affects section sizes which in turn affects section offsets. + for (SectionBase &Sec : Obj.sections()) + if (auto StrTab = dyn_cast(&Sec)) + StrTab->prepareForLayout(); + + assignOffsets(); + + // layoutSections could have modified section indexes, so we need + // to fill the index table after assignOffsets. + if (Obj.SymbolTable != nullptr) + Obj.SymbolTable->fillShndxTable(); + + // Finally now that all offsets and indexes have been set we can finalize any + // remaining issues. + uint64_t Offset = Obj.SHOff + sizeof(Elf_Shdr); + for (SectionBase &Sec : Obj.sections()) { + Sec.HeaderOffset = Offset; + Offset += sizeof(Elf_Shdr); + if (WriteSectionHeaders) + Sec.NameIndex = Obj.SectionNames->findIndex(Sec.Name); + Sec.finalize(); + } + + if (Error E = Buf.allocate(totalSize())) + return E; + SecWriter = std::make_unique>(Buf); + return Error::success(); +} + +Error BinaryWriter::write() { + for (const SectionBase &Sec : Obj.allocSections()) + Sec.accept(*SecWriter); + return Buf.commit(); +} + +Error BinaryWriter::finalize() { + // Compute the section LMA based on its sh_offset and the containing segment's + // p_offset and p_paddr. Also compute the minimum LMA of all non-empty + // sections as MinAddr. In the output, the contents between address 0 and + // MinAddr will be skipped. + uint64_t MinAddr = UINT64_MAX; + for (SectionBase &Sec : Obj.allocSections()) { + if (Sec.ParentSegment != nullptr) + Sec.Addr = + Sec.Offset - Sec.ParentSegment->Offset + Sec.ParentSegment->PAddr; + if (Sec.Size > 0) + MinAddr = std::min(MinAddr, Sec.Addr); + } + + // Now that every section has been laid out we just need to compute the total + // file size. This might not be the same as the offset returned by + // layoutSections, because we want to truncate the last segment to the end of + // its last non-empty section, to match GNU objcopy's behaviour. + TotalSize = 0; + for (SectionBase &Sec : Obj.allocSections()) + if (Sec.Type != SHT_NOBITS && Sec.Size > 0) { + Sec.Offset = Sec.Addr - MinAddr; + TotalSize = std::max(TotalSize, Sec.Offset + Sec.Size); + } + + if (Error E = Buf.allocate(TotalSize)) + return E; + SecWriter = std::make_unique(Buf); + return Error::success(); +} + +bool IHexWriter::SectionCompare::operator()(const SectionBase *Lhs, + const SectionBase *Rhs) const { + return (sectionPhysicalAddr(Lhs) & 0xFFFFFFFFU) < + (sectionPhysicalAddr(Rhs) & 0xFFFFFFFFU); +} + +uint64_t IHexWriter::writeEntryPointRecord(uint8_t *Buf) { + IHexLineData HexData; + uint8_t Data[4] = {}; + // We don't write entry point record if entry is zero. + if (Obj.Entry == 0) + return 0; + + if (Obj.Entry <= 0xFFFFFU) { + Data[0] = ((Obj.Entry & 0xF0000U) >> 12) & 0xFF; + support::endian::write(&Data[2], static_cast(Obj.Entry), + support::big); + HexData = IHexRecord::getLine(IHexRecord::StartAddr80x86, 0, Data); + } else { + support::endian::write(Data, static_cast(Obj.Entry), + support::big); + HexData = IHexRecord::getLine(IHexRecord::StartAddr, 0, Data); + } + memcpy(Buf, HexData.data(), HexData.size()); + return HexData.size(); +} + +uint64_t IHexWriter::writeEndOfFileRecord(uint8_t *Buf) { + IHexLineData HexData = IHexRecord::getLine(IHexRecord::EndOfFile, 0, {}); + memcpy(Buf, HexData.data(), HexData.size()); + return HexData.size(); +} + +Error IHexWriter::write() { + IHexSectionWriter Writer(Buf); + // Write sections. + for (const SectionBase *Sec : Sections) + Sec->accept(Writer); + + uint64_t Offset = Writer.getBufferOffset(); + // Write entry point address. + Offset += writeEntryPointRecord(Buf.getBufferStart() + Offset); + // Write EOF. + Offset += writeEndOfFileRecord(Buf.getBufferStart() + Offset); + assert(Offset == TotalSize); + return Buf.commit(); +} + +Error IHexWriter::checkSection(const SectionBase &Sec) { + uint64_t Addr = sectionPhysicalAddr(&Sec); + if (addressOverflows32bit(Addr) || addressOverflows32bit(Addr + Sec.Size - 1)) + return createStringError( + errc::invalid_argument, + "Section '%s' address range [0x%llx, 0x%llx] is not 32 bit", + Sec.Name.c_str(), Addr, Addr + Sec.Size - 1); + return Error::success(); +} + +Error IHexWriter::finalize() { + bool UseSegments = false; + auto ShouldWrite = [](const SectionBase &Sec) { + return (Sec.Flags & ELF::SHF_ALLOC) && (Sec.Type != ELF::SHT_NOBITS); + }; + auto IsInPtLoad = [](const SectionBase &Sec) { + return Sec.ParentSegment && Sec.ParentSegment->Type == ELF::PT_LOAD; + }; + + // We can't write 64-bit addresses. + if (addressOverflows32bit(Obj.Entry)) + return createStringError(errc::invalid_argument, + "Entry point address 0x%llx overflows 32 bits.", + Obj.Entry); + + // If any section we're to write has segment then we + // switch to using physical addresses. Otherwise we + // use section virtual address. + for (const SectionBase &Sec : Obj.sections()) + if (ShouldWrite(Sec) && IsInPtLoad(Sec)) { + UseSegments = true; + break; + } + + for (const SectionBase &Sec : Obj.sections()) + if (ShouldWrite(Sec) && (!UseSegments || IsInPtLoad(Sec))) { + if (Error E = checkSection(Sec)) + return E; + Sections.insert(&Sec); + } + + IHexSectionWriterBase LengthCalc(Buf); + for (const SectionBase *Sec : Sections) + Sec->accept(LengthCalc); + + // We need space to write section records + StartAddress record + // (if start adress is not zero) + EndOfFile record. + TotalSize = LengthCalc.getBufferOffset() + + (Obj.Entry ? IHexRecord::getLineLength(4) : 0) + + IHexRecord::getLineLength(0); + if (Error E = Buf.allocate(TotalSize)) + return E; + return Error::success(); +} + +template class ELFBuilder; +template class ELFBuilder; +template class ELFBuilder; +template class ELFBuilder; + +template class ELFWriter; +template class ELFWriter; +template class ELFWriter; +template class ELFWriter; + +} // end namespace elf +} // end namespace objectcopy +} // end namespace llvm diff --git a/llvm/lib/LLVMBuild.txt b/llvm/lib/ObjectCopy/LLVMBuild.txt copy from llvm/lib/LLVMBuild.txt copy to llvm/lib/ObjectCopy/LLVMBuild.txt --- a/llvm/lib/LLVMBuild.txt +++ b/llvm/lib/ObjectCopy/LLVMBuild.txt @@ -1,4 +1,4 @@ -;===- ./lib/LLVMBuild.txt --------------------------------------*- Conf -*--===; +;===- ./lib/ObjectCopy/LLVMBuild.txt ---------------*- Conf -*--===; ; ; Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. ; See https://llvm.org/LICENSE.txt for license information. @@ -14,46 +14,8 @@ ; ;===------------------------------------------------------------------------===; -[common] -subdirectories = - Analysis - AsmParser - Bitcode - Bitstream - CodeGen - DebugInfo - Demangle - DWARFLinker - ExecutionEngine - Extensions - Frontend - FuzzMutate - LineEditor - Linker - InterfaceStub - IR - IRReader - LTO - MC - MCA - Object - BinaryFormat - ObjectYAML - Option - Remarks - Passes - ProfileData - Support - TableGen - TextAPI - Target - Testing - ToolDrivers - Transforms - WindowsManifest - XRay - [component_0] -type = Group -name = Libraries -parent = $ROOT +type = Library +name = ObjectCopy +parent = Libraries +required_libraries = Object Support MC diff --git a/llvm/tools/dsymutil/DwarfLinkerForBinary.h b/llvm/tools/dsymutil/DwarfLinkerForBinary.h --- a/llvm/tools/dsymutil/DwarfLinkerForBinary.h +++ b/llvm/tools/dsymutil/DwarfLinkerForBinary.h @@ -144,7 +144,13 @@ /// @} bool hasValidRelocationAt(uint64_t StartOffset, uint64_t EndOffset, - CompileUnit::DIEInfo &Info) override; + CompileUnit::DIEInfo &Info); + + bool isLiveLocationAttribute(const DWARFDie &DIE, dwarf::Attribute Attr, + CompileUnit::DIEInfo &) override; + + bool isLiveAddressAttribute(const DWARFDie &DIE, dwarf::Attribute Attr, + CompileUnit::DIEInfo &) override; bool applyValidRelocs(MutableArrayRef Data, uint64_t BaseOffset, bool IsLittleEndian) override; diff --git a/llvm/tools/dsymutil/DwarfLinkerForBinary.cpp b/llvm/tools/dsymutil/DwarfLinkerForBinary.cpp --- a/llvm/tools/dsymutil/DwarfLinkerForBinary.cpp +++ b/llvm/tools/dsymutil/DwarfLinkerForBinary.cpp @@ -649,6 +649,58 @@ return true; } +/// Get the starting and ending (exclusive) offset for the +/// attribute with index \p Idx descibed by \p Abbrev. \p Offset is +/// supposed to point to the position of the first attribute described +/// by \p Abbrev. +/// \return [StartOffset, EndOffset) as a pair. +static std::pair +getAttributeOffsets(const DWARFAbbreviationDeclaration *Abbrev, unsigned Idx, + uint64_t Offset, const DWARFUnit &Unit) { + DataExtractor Data = Unit.getDebugInfoExtractor(); + + for (unsigned I = 0; I < Idx; ++I) + DWARFFormValue::skipValue(Abbrev->getFormByIndex(I), Data, &Offset, + Unit.getFormParams()); + + uint64_t End = Offset; + DWARFFormValue::skipValue(Abbrev->getFormByIndex(Idx), Data, &End, + Unit.getFormParams()); + + return std::make_pair(Offset, End); +} + +bool DwarfLinkerForBinary::AddressManager::isLiveLocationAttribute( + const DWARFDie &DIE, dwarf::Attribute Attr, CompileUnit::DIEInfo &MyInfo) { + + const auto *Abbrev = DIE.getAbbreviationDeclarationPtr(); + + Optional LocationIdx = Abbrev->findAttributeIndex(Attr); + assert(LocationIdx && "Attr attribute is missed"); + + uint64_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode()); + uint64_t LocationOffset, LocationEndOffset; + std::tie(LocationOffset, LocationEndOffset) = + getAttributeOffsets(Abbrev, *LocationIdx, Offset, *DIE.getDwarfUnit()); + + return hasValidRelocationAt(LocationOffset, LocationEndOffset, MyInfo); +} + +bool DwarfLinkerForBinary::AddressManager::isLiveAddressAttribute( + const DWARFDie &DIE, dwarf::Attribute Attr, CompileUnit::DIEInfo &MyInfo) { + const auto *Abbrev = DIE.getAbbreviationDeclarationPtr(); + + Optional LowPcIdx = Abbrev->findAttributeIndex(Attr); + assert(LowPcIdx && "Attr attribute is missed"); + + uint64_t Offset = DIE.getOffset() + getULEB128Size(Abbrev->getCode()); + uint64_t LowPcOffset, LowPcEndOffset; + std::tie(LowPcOffset, LowPcEndOffset) = + getAttributeOffsets(Abbrev, *LowPcIdx, Offset, *DIE.getDwarfUnit()); + + return hasValidRelocationAt(LowPcOffset, LowPcEndOffset, MyInfo); +} + /// Apply the valid relocations found by findValidRelocs() to /// the buffer \p Data, taking into account that Data is at \p BaseOffset /// in the debug_info section. diff --git a/llvm/tools/llvm-dwarfutil/CMakeLists.txt b/llvm/tools/llvm-dwarfutil/CMakeLists.txt new file mode 100644 --- /dev/null +++ b/llvm/tools/llvm-dwarfutil/CMakeLists.txt @@ -0,0 +1,18 @@ +set(LLVM_LINK_COMPONENTS + ${LLVM_TARGETS_TO_BUILD} + DebugInfoDWARF + DWARFLinker + ObjectCopy + MC + Object + Support + Target + AllTargetsCodeGens + AllTargetsDescs + AllTargetsInfos + ) + +add_llvm_tool(llvm-dwarfutil + llvm-dwarfutil.cpp + DebugInfoLinker.cpp + ) diff --git a/llvm/tools/llvm-dwarfutil/DebugInfoLinker.h b/llvm/tools/llvm-dwarfutil/DebugInfoLinker.h new file mode 100644 --- /dev/null +++ b/llvm/tools/llvm-dwarfutil/DebugInfoLinker.h @@ -0,0 +1,24 @@ +//===-- DebugInfoLinker.h -------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_TOOLS_LLVM_DWARFUTIL_DEBUGINFOLINKER_H +#define LLVM_TOOLS_LLVM_DWARFUTIL_DEBUGINFOLINKER_H + +#include "Options.h" +#include "llvm/Object/Archive.h" +#include "llvm/Object/ELFObjectFile.h" +#include "llvm/Object/ObjectFile.h" + +using namespace llvm; +using namespace object; +using namespace dwarfutil; + +bool link(ObjectFile &file, const Options &Options, DataBits &LinkedDebugInfo); + +#endif // LLVM_TOOLS_LLVM_DWARFUTIL_DEBUGINFOLINKER_H diff --git a/llvm/tools/llvm-dwarfutil/DebugInfoLinker.cpp b/llvm/tools/llvm-dwarfutil/DebugInfoLinker.cpp new file mode 100644 --- /dev/null +++ b/llvm/tools/llvm-dwarfutil/DebugInfoLinker.cpp @@ -0,0 +1,187 @@ +//===-- DebugInfoLinker.cpp -----------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "DebugInfoLinker.h" +#include "Error.h" +#include "llvm/DWARFLinker/DWARFLinker.h" +#include "llvm/DWARFLinker/DWARFStreamer.h" +#include "llvm/Object/ObjectFile.h" + +using namespace llvm; +using namespace object; +using namespace dwarfutil; + +bool isDeadAddressRange(uint64_t LowPC, uint64_t HighPC, uint16_t Version, + TombstoneKind Tomstone) { + switch (Tomstone) { + case TombstoneKind::BFD: + return (Version <= 4) ? ((LowPC == 1 && HighPC == 1) || LowPC == 0) + : (LowPC == 0); + break; + case TombstoneKind::MaxPC: + return (Version <= 4) ? (LowPC == UINT64_MAX - 1) : (LowPC == UINT64_MAX); + break; + + default: + llvm_unreachable("Unknown tombstone value"); + } +} + +bool isDeadLocationAddress(uint64_t LowPC, TombstoneKind Tomstone) { + switch (Tomstone) { + case TombstoneKind::BFD: + return (LowPC == 0 || LowPC == UINT64_MAX); + break; + case TombstoneKind::MaxPC: + return (LowPC == UINT64_MAX); + break; + + default: + llvm_unreachable("Unknown tombstone value"); + } +} + +class ObjFileAddressMap : public AddressesMap { +public: + ObjFileAddressMap(DWARFContext &Context, const Options &Options) + : Opts(Options) { + for (std::unique_ptr &CU : Context.compile_units()) { + Expected ARanges = + CU->getUnitDIE().getAddressRanges(); + if (ARanges) { + for (auto &Range : *ARanges) { + if (!isDeadAddressRange(Range.LowPC, Range.HighPC, CU->getVersion(), + Options.Tombstone)) + AddressRanges[{Range.LowPC}] = {Range.HighPC, 0}; + } + } + } + HasLiveAddressRanges = !AddressRanges.empty(); + } + + bool areRelocationsResolved() const override { return true; } + + // should be renamed into has valid address ranges + bool hasValidRelocs(bool) override { return HasLiveAddressRanges; } + + bool isLiveLocationAttribute(const DWARFDie &DIE, dwarf::Attribute Attr, + CompileUnit::DIEInfo &Info) override { + + if (Expected> Loc = + DIE.getLocations(Attr)) { + DWARFUnit *U = DIE.getDwarfUnit(); + for (const auto &Entry : *Loc) { + DataExtractor Data(toStringRef(Entry.Expr), + U->getContext().isLittleEndian(), 0); + DWARFExpression Expression(Data, U->getAddressByteSize(), + U->getFormParams().Format); + bool HasLiveAddresses = + any_of(Expression, [&](DWARFExpression::Operation &Op) { + // TODO: add handling of dwarf::DW_OP_addrx + return !Op.isError() && + (Op.getCode() == dwarf::DW_OP_addr && + !isDeadLocationAddress(Op.getRawOperand(0), + Opts.Tombstone)); + }); + + if (HasLiveAddresses) { + Info.AddrAdjust = 0; + Info.InDebugMap = true; + return true; + } + } + } + + return false; + } + + bool isLiveAddressAttribute(const DWARFDie &DIE, dwarf::Attribute Attr, + CompileUnit::DIEInfo &Info) override { + if (Optional LowPC = dwarf::toAddress(DIE.find(Attr))) { + if (isInLiveAddressRange(*LowPC)) { + Info.AddrAdjust = 0; + Info.InDebugMap = true; + return true; + } + } + + return false; + } + + bool applyValidRelocs(MutableArrayRef, uint64_t, bool) override { + // no need to apply relocations to the linked binary. + return false; + } + + RangesTy &getValidAddressRanges() override { return AddressRanges; }; + + void clear() override { AddressRanges.clear(); } + +private: + struct SymbolAddresses { + uint64_t ObjectAddress = 0; + uint64_t InputSectionAddress = 0; + }; + + RangesTy AddressRanges; + bool HasLiveAddressRanges = false; + const Options &Opts; +}; + +bool link(ObjectFile &File, const Options &Options, DataBits &LinkedDebugInfo) { + + auto ReportWarn = [&](const Twine &Message, StringRef Context, + const DWARFDie *) { + if (Context.empty()) + warn(Message); + else + warn(Context + ": " + Message); + }; + + // Create output streamer. + raw_svector_ostream Stream(LinkedDebugInfo); + DwarfStreamer OutStreamer(OutputFileType::Object, Stream, + [](StringRef Input) { return Input; }, false, + ReportWarn, ReportWarn); + if (!OutStreamer.init(Triple(sys::getProcessTriple()))) + return false; + + // Create DWARF linker. + DWARFLinker DebugInfoLinker(&OutStreamer, DwarfLinkerClient::LLD); + + DebugInfoLinker.setEstimatedObjfilesAmount(1); + DebugInfoLinker.setAccelTableKind(AccelTableKind::None); + DebugInfoLinker.setErrorHandler(ReportWarn); + DebugInfoLinker.setWarningHandler(ReportWarn); + DebugInfoLinker.setNumThreads(Options.NumThreads); + DebugInfoLinker.setNoODR(Options.NoDeduplicateTypes); + DebugInfoLinker.setVerbosity(Options.Verbose); + + std::vector> ObjectsForLinking(1); + std::vector> AddresssMapForLinking(1); + std::vector EmptyWarnings; + + std::unique_ptr Context = DWARFContext::create(File); + + // Add object files to the DWARFLinker. + AddresssMapForLinking[0] = + std::make_unique(*Context, Options); + + ObjectsForLinking[0] = std::make_unique( + File.getFileName(), &*Context, AddresssMapForLinking[0].get(), + EmptyWarnings); + + for (size_t I = 0; I < ObjectsForLinking.size(); I++) + DebugInfoLinker.addObjectFile(*ObjectsForLinking[I]); + + // Link debug info. + DebugInfoLinker.link(); + OutStreamer.finish(); + return true; +} diff --git a/llvm/tools/llvm-dwarfutil/Error.h b/llvm/tools/llvm-dwarfutil/Error.h new file mode 100644 --- /dev/null +++ b/llvm/tools/llvm-dwarfutil/Error.h @@ -0,0 +1,41 @@ +//===-- Error.h -----------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_TOOLS_LLVM_DWARFUTIL_ERROR_H +#define LLVM_TOOLS_LLVM_DWARFUTIL_ERROR_H + +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/ADT/StringSet.h" +#include "llvm/ADT/Triple.h" +#include "llvm/Support/Debug.h" +#include "llvm/Support/Error.h" +#include "llvm/Support/Format.h" +#include "llvm/Support/raw_ostream.h" + +using namespace llvm; + +static inline void error(StringRef Prefix, Error Err) { + if (!Err) + return; + errs() << Prefix << ": " << Err << "\n"; + consumeError(std::move(Err)); + exit(1); +} + +static inline void error(StringRef Prefix, std::error_code EC) { + if (!EC) + return; + errs() << Prefix << ": " << EC.message() << "\n"; + exit(1); +} + +static inline void warn(const Twine &Message) { outs() << Message << "\n"; } + +#endif // LLVM_TOOLS_LLVM_DWARFUTIL_ERROR_H diff --git a/llvm/tools/llvm-dwarfutil/Options.h b/llvm/tools/llvm-dwarfutil/Options.h new file mode 100644 --- /dev/null +++ b/llvm/tools/llvm-dwarfutil/Options.h @@ -0,0 +1,50 @@ +//===-- Options.h ---------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_TOOLS_LLVM_DWARFUTIL_OPTIONS_H +#define LLVM_TOOLS_LLVM_DWARFUTIL_OPTIONS_H + +#include "llvm/ADT/SmallString.h" +#include "llvm/ADT/StringRef.h" + +using namespace llvm; + +namespace dwarfutil { + +/// The kind of tombstone value. +enum class TombstoneKind { + BFD, ///< Bfd default. + MaxPC, ///< Dwarf v6. +}; + +struct Options { + StringRef InputFileName; + StringRef OutputFileName; + bool RemoveObsoleteDebugInfo = false; + bool NoDeduplicateTypes = false; + bool Decompress = false; + bool Compress = false; + bool BuildARanges = false; + bool BuildDebugNames = false; + bool BuildDebugPubNames = false; + bool BuildDebugPubTypes = false; + bool BuildGdbIndex = false; + bool StripAll = false; + bool StripUnoptimizedDebug = false; + TombstoneKind Tombstone = TombstoneKind::MaxPC; + bool Verbose = false; + int NumThreads = 0; + bool Verify = false; +}; + +typedef SmallString<512000> DataBits; + +} // namespace dwarfutil + +#endif // LLVM_TOOLS_LLVM_DWARFUTIL_OPTIONS_H diff --git a/llvm/tools/llvm-dwarfutil/llvm-dwarfutil.cpp b/llvm/tools/llvm-dwarfutil/llvm-dwarfutil.cpp new file mode 100644 --- /dev/null +++ b/llvm/tools/llvm-dwarfutil/llvm-dwarfutil.cpp @@ -0,0 +1,391 @@ +//===-- dwarfutil.cpp - DWARF optimizing utility for llvm -----------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "DebugInfoLinker.h" +#include "Error.h" +#include "Options.h" +#include "llvm/MC/MCTargetOptionsCommandFlags.h" +#include "llvm/ObjectCopy/Buffer.h" +#include "llvm/ObjectCopy/CopyConfig.h" +#include "llvm/ObjectCopy/ELF/ELFObjcopy.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/PrettyStackTrace.h" +#include "llvm/Support/Signals.h" +#include "llvm/Support/TargetSelect.h" + +using namespace llvm; +using namespace object; + +/// @} +/// Command line options. +/// @{ + +namespace { +using namespace cl; + +OptionCategory DwarfUtilOptions("DwarfUtil Options"); + +static opt BuildAranges("build-aranges", + desc("generate .debug_aranges table."), + cat(DwarfUtilOptions)); + +static opt BuildDebugNames("build-debug-names", + desc("generate .debug_names table."), + cat(DwarfUtilOptions)); + +static opt BuildGDBIndex("build-gdb-index", + desc("generate .gdb_index table."), + cat(DwarfUtilOptions)); + +static opt BuildDebugPubnames("build-debug-pubnames", + desc("generate .debug_pubnames table."), + cat(DwarfUtilOptions)); + +static opt BuildDebugPubtypes("build-debug-pubtypes", + desc("generate .debug_pubtypes table."), + cat(DwarfUtilOptions)); + +static opt Compress("compress", desc("Compress debug tables."), + cat(DwarfUtilOptions)); +static alias Z("z", desc("Alias for --compress."), aliasopt(Compress)); + +static opt Decompress("decompress", desc("Decompress debug tables."), + cat(DwarfUtilOptions)); +static alias X("x", desc("Alias for --decompress."), aliasopt(Decompress)); + +static opt + NoDeduplicateTypes("no-deduplicate-types", + desc("Do not do ODR deduplication for debug types."), + init(false), cat(DwarfUtilOptions)); +static alias DT("no-odr", desc("Alias for --no-deduplicate-types."), + aliasopt(NoDeduplicateTypes)); + +static opt + RemoveObsoleteDebugInfo("garbage-collect", + desc("Do garbage collecting for debug info."), + init(true), cat(DwarfUtilOptions)); +static alias GC("gc", desc("Alias for --garbage-collect."), + aliasopt(RemoveObsoleteDebugInfo)); + +static opt Help("h", desc("Alias for --help."), init(false), + cat(DwarfUtilOptions)); + +static cl::opt InputFilename(cl::Positional, + cl::desc("")); + +static cl::opt OutputFilename("out", desc(""), + cat(DwarfUtilOptions)); +static alias OF("o", desc("Alias for --out."), aliasopt(OutputFilename)); + +static opt + NumThreads("num-threads", + desc("Specify the maximum number (n) of simultaneous threads " + "to use when optimizing input file.\nDefaults to the " + "number of cores on the current machine."), + value_desc("n"), init(0), cat(DwarfUtilOptions)); +static alias J("j", desc("Alias for --num-threads."), aliasopt(NumThreads)); + +static opt StripAll("strip-all", desc("Strip all debug tables."), + cat(DwarfUtilOptions)); + +static alias S("s", desc("Alias for --strip-all."), aliasopt(StripAll)); + +static opt + StripUnoptimizedDebug("strip-unoptimized-debug", + desc("Strip all unoptimized debug tables."), + init(true), cat(DwarfUtilOptions)); + +static opt Tombstone( + "tombstone", desc("Tombstone value used as a marker of invalid address."), + values(clEnumValN(TombstoneKind::BFD, "bfd", "BFD default value."), + clEnumValN(TombstoneKind::MaxPC, "maxpc", "Max PC values(-1/-2).")), + init(TombstoneKind::BFD), cat(DwarfUtilOptions)); + +static opt Verbose("verbose", + desc("Enable verbose logging and encoding details."), + cat(DwarfUtilOptions)); + +static opt + Verify("verify", + desc("Run the DWARF verifier on the linked DWARF debug info."), + init(false), cat(DwarfUtilOptions)); + +static opt Version("v", desc("Alias for --version."), init(false), + cat(DwarfUtilOptions)); + +std::string ToolName; + +} // namespace +/// @} + +static mc::RegisterMCTargetOptionsFlags MOF; + +void setOptions(Options &Options) { + Options.BuildARanges = BuildAranges; + Options.BuildDebugNames = BuildDebugNames; + Options.BuildGdbIndex = BuildGDBIndex; + Options.BuildDebugPubNames = BuildDebugPubnames; + Options.BuildDebugPubTypes = BuildDebugPubtypes; + Options.Compress = Compress; + Options.Decompress = Decompress; + Options.InputFileName = InputFilename; + Options.NoDeduplicateTypes = NoDeduplicateTypes; + Options.NumThreads = NumThreads; + Options.OutputFileName = OutputFilename; + Options.RemoveObsoleteDebugInfo = RemoveObsoleteDebugInfo; + Options.StripAll = StripAll; + Options.StripUnoptimizedDebug = StripUnoptimizedDebug; + Options.Tombstone = Tombstone; + Options.Verbose = Verbose; + Options.Verify = Verify; + + if (Options.Verbose) { + if (Options.NumThreads != 1) + warn("--num-threads set to 1 because Verbose mode is specified."); + + Options.NumThreads = 1; + } + + if (Options.Compress && Options.Decompress) { + warn("--compress and --decompress may not occur together."); + exit(1); + } + + if (Options.StripAll && Options.StripUnoptimizedDebug) { + if (StripUnoptimizedDebug.getNumOccurrences() > 0) { + warn("--strip-all and --strip-unoptimized-debug may not occur together."); + exit(1); + } else { + Options.StripUnoptimizedDebug = false; + } + } + + if (Options.BuildARanges) { + warn("--build-aranges is not implemented yet."); + exit(1); + } + + if (Options.BuildDebugNames) { + warn("--build-debug-names is not implemented yet."); + exit(1); + } + + if (Options.BuildGdbIndex) { + warn("--build-debug-names is not implemented yet."); + exit(1); + } + + if (Options.BuildDebugPubNames) { + warn("--build-debug-pubnames is not implemented yet."); + exit(1); + } + + if (Options.BuildDebugPubTypes) { + warn("--build-debug-pubtypes is not implemented yet."); + exit(1); + } + + if (Options.Decompress && Options.StripAll) { + warn("--strip-all and --decompress may not occur together."); + exit(1); + } + + if (Options.Verify) { + warn("--verify is not implemented yet."); + exit(1); + } + + if (Options.Decompress) { + if (Options.StripAll) { + warn("--decompress and --strip-all may not occur together."); + exit(1); + } else if (Options.RemoveObsoleteDebugInfo) { + if (RemoveObsoleteDebugInfo.getNumOccurrences() > 0) { + warn("--decompress and --garbage-collect may not occur together."); + exit(1); + } else { + Options.RemoveObsoleteDebugInfo = false; + } + } else if (Options.BuildARanges || Options.BuildGdbIndex || + Options.BuildDebugNames || Options.BuildDebugPubNames || + Options.BuildDebugPubTypes) { + warn("--decompress may not occur together with other options yet."); + exit(1); + } + } else if (Options.StripAll) { + if (RemoveObsoleteDebugInfo.getNumOccurrences() > 0) { + warn("--strip-all and --garbage-collect may not occur together."); + exit(1); + } else { + Options.RemoveObsoleteDebugInfo = false; + } + } else { + if (!Options.RemoveObsoleteDebugInfo && + RemoveObsoleteDebugInfo.getNumOccurrences() > 0) { + warn("switching off for --garbage-collect is not currently supported. " + "switched on."); + Options.RemoveObsoleteDebugInfo = true; + } + } +} + +template +Error writeOutputFile(const Options &Options, ELFObjectFile &InputFile, + DataBits &OutBits) { + objectcopy::CopyConfig Config; + + Config.InputFilename = Options.InputFileName; + Config.OutputFilename = Options.OutputFileName; + if (Options.Compress) + Config.CompressionType = DebugCompressionType::GNU; + Config.DecompressDebugSections = Options.Decompress; + + if (Options.StripAll) { + Config.StripDebug = true; + } else if (Options.Decompress) { + Config.DecompressDebugSections = true; + } else { + if (Expected> MemFile = + ELFObjectFile::create(MemoryBufferRef(OutBits, ""))) { + if (Options.StripUnoptimizedDebug) { + // Delete all debug sections. Debug sections which have optimized + // pair should be deleted as they would be replaced with paired + // optimized version. Debug section which does not have optimized pair + // should be deleted since they could become incorrect after debug + // info has been optimized. + Error Err = + Config.ToRemove.addMatcher(objectcopy::NameOrPattern::create( + "\\.debug*", objectcopy::MatchStyle::Wildcard, + [](Error Err) -> Error { return Err; })); + error(ToolName, std::move(Err)); + + Err = Config.ToRemove.addMatcher(objectcopy::NameOrPattern::create( + "\\.gdb_index", objectcopy::MatchStyle::Wildcard, + [](Error Err) -> Error { return Err; })); + error(ToolName, std::move(Err)); + } else { + // Delete debug sections which have optimized pair as they should be + // replaced with paired optimized version. + for (const auto &Section : MemFile->sections()) { + if (Expected SectionName = Section.getName()) { + if (SectionName->startswith(".debug") || + *SectionName == ".gdb_index") { + Error Err = + Config.ToRemove.addMatcher(objectcopy::NameOrPattern::create( + *SectionName, objectcopy::MatchStyle::Wildcard, + [](Error Err) -> Error { return Err; })); + error(ToolName, std::move(Err)); + } + } + } + } + + // Add new debug sections. + for (const SectionRef &Sec : MemFile->sections()) { + if (Expected SecName = Sec.getName()) + if (SecName->startswith(".debug") || *SecName == ".gdb_index") { + if (Expected SecData = Sec.getContents()) { + Config.NewDebugSections.insert( + std::make_pair(*SecName, *SecData)); + } + } + } + } else { + error(ToolName, createStringError(std::errc::invalid_argument, + "unknown command line option.")); + } + } + + objectcopy::FileBuffer FB(Config.OutputFilename); + return objectcopy::elf::executeObjcopyOnBinary(Config, InputFile, FB); +} + +template +void linkDebugInfoAndWriteOutputFile(const Options &Options, + ELFObjectFile &InputFile, + DataBits &OutBits) { + if (Options.StripAll || Options.Decompress || + link(InputFile, Options, OutBits)) { + if (Error E = writeOutputFile(Options, InputFile, OutBits)) + error(ToolName, std::move(E)); + } else + error(ToolName, createStringError(std::errc::invalid_argument, + "possible broken debug info.")); +} + +int main(int Argc, char const *Argv[]) { + // Print a stack trace if we signal out. + sys::PrintStackTraceOnErrorSignal(Argv[0]); + PrettyStackTraceProgram X(Argc, Argv); + llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. + int NumArgs = Argc; + ToolName = Argv[0]; + + const char *Overview = "A tool for optimizing debug info.\n"; + HideUnrelatedOptions({&DwarfUtilOptions}); + cl::ParseCommandLineOptions(NumArgs, Argv, Overview); + + if (Help || (NumArgs == 1)) { + PrintHelpMessage(/*Hidden =*/false, /*Categorized =*/true); + return 0; + } else if (Version) { + PrintVersionMessage(); + return 0; + } else if (InputFilename.empty()) { + error(ToolName, createStringError(std::errc::invalid_argument, + "please specify input file.")); + } else if (InputFilename != "-" && !sys::fs::exists(InputFilename)) { + std::string Message; + Message = "specified input file \""; + Message += InputFilename; + Message += "\" does not exist."; + error(ToolName, + createStringError(std::errc::invalid_argument, Message.c_str())); + } else if (OutputFilename.empty()) { + error(ToolName, createStringError(std::errc::invalid_argument, + "please specify output file.")); + } + + InitializeAllTargets(); + InitializeAllTargetMCs(); + InitializeAllTargetInfos(); + InitializeAllAsmPrinters(); + InitializeAllAsmParsers(); + + ErrorOr> BuffOrErr = + MemoryBuffer::getFileOrSTDIN(InputFilename); + error(InputFilename, BuffOrErr.getError()); + + Options Opts; + + setOptions(Opts); + + Expected> BinOrErr = + object::createBinary(**BuffOrErr); + error(InputFilename, errorToErrorCode(BinOrErr.takeError())); + + DataBits OptimizedData; + + if (ELFObjectFile *O = + dyn_cast>(BinOrErr->get())) { + linkDebugInfoAndWriteOutputFile(Opts, *O, OptimizedData); + } else if (ELFObjectFile *O = + dyn_cast>(BinOrErr->get())) { + linkDebugInfoAndWriteOutputFile(Opts, *O, OptimizedData); + } else if (ELFObjectFile *O = + dyn_cast>(BinOrErr->get())) { + linkDebugInfoAndWriteOutputFile(Opts, *O, OptimizedData); + } else if (ELFObjectFile *O = + dyn_cast>(BinOrErr->get())) { + linkDebugInfoAndWriteOutputFile(Opts, *O, OptimizedData); + } else + llvm_unreachable("Bad file format"); + + return EXIT_SUCCESS; +}