Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport lld fixes from LLVM 15 #13

Merged
merged 85 commits into from
Jan 6, 2023
Merged
Changes from 1 commit
Commits
Show all changes
85 commits
Select commit Hold shift + click to select a range
c5b7e27
[lld-macho][nfc] Hoist out creation of Section in parseSections()
int3 Feb 2, 2022
ac5e23f
[lld-macho] Fix duplicate symbols with relocatable objects
keith Jan 29, 2022
0e550c4
[lld-macho] Minor clean up: use .find() to check for key existence ra…
oontvoo Feb 3, 2022
428e264
[lld-macho][nfc] Eliminate InputSection::Shared
int3 Feb 4, 2022
1546340
[lld][macho] Add more skip platform check for libSystem re-exports
bc-lee Feb 4, 2022
ede4fe0
[lld-macho] Add -pagezero_size
Link1J Feb 6, 2022
1cc07cc
[lld-macho] Include address offsets in error messages
int3 Feb 8, 2022
c4359b2
Print C-string literals in mapfile
Feb 12, 2022
cbc561b
[lld-macho] Unset ExportDynamic where possible for LTO
int3 Feb 12, 2022
5878c98
[lld-macho][nfc] Factor out callgraph parsing code
int3 Feb 16, 2022
f07d787
[lld-macho] Don't include CommandFlags.h in CommonLinkerContext.h
int3 Feb 17, 2022
2da75dd
[lld-macho] Allow order files and call graph sorting to be used together
speednoisemovement Jan 14, 2022
86c5e65
[lld-macho] Improve hiding of unnamed_addr symbols
int3 Feb 18, 2022
2dc406b
[lld-macho][nfc] Move ICF-specific logic into ICF.cpp
int3 Feb 23, 2022
b40f9cd
[lld-macho][nfc] Refactor MarkLive
int3 Feb 23, 2022
f0ae869
[lld-macho] Implement -why_live (without perf overhead)
int3 Feb 24, 2022
82888fc
[lld/MachO] Fix +asserts build after recent change
rnk Feb 24, 2022
f32c8a0
[lld-macho] Have relocation address included in range-check error mes…
int3 Mar 1, 2022
af3ae34
[lld-macho][nfc] Remove file statics from ICF.cpp
int3 Mar 7, 2022
6ade364
[lld-macho][nfc] Use llvm::function_ref instead of std::function
int3 Mar 5, 2022
dbc294c
[lld-macho][nfc] Track # of ICF calls to `equals*` methods
int3 Mar 7, 2022
65c4433
[lld-macho][nfc] Rename isec -> referentIsec to avoid shadowing
int3 Mar 7, 2022
63d70dd
[lld-macho][nfc] Don't use `stubsHelperIndex` in ICF hash
int3 Mar 7, 2022
e3b6822
[lld-macho][nfc] Reduce size of icfEqClass hash
int3 Mar 7, 2022
9439228
Revert "[lld-macho][nfc] Don't use `stubsHelperIndex` in ICF hash"
nico Mar 7, 2022
0e03fcb
Re-land [lld-macho][nfc] Don't use `stubsHelperIndex` in ICF hash
int3 Mar 7, 2022
88ad6d2
[lld-macho] Deduplicate CFStrings during ICF
int3 Mar 8, 2022
f06266c
[lld-macho] Deduplicate the `__objc_classrefs` section contents
int3 Mar 8, 2022
728ce76
[lld-macho] Align cstrings less conservatively
int3 Mar 10, 2022
8247adf
[lld-macho][nfc] Fix formatting in ld64-vs-lld.rst
int3 Mar 10, 2022
e88e11b
[lld-macho] Don't allocate memory in parallelForEach
int3 Mar 11, 2022
93ec01d
[lld-macho] Avoid using bump-alloc in TrieBuider
oontvoo Mar 14, 2022
fb74624
Revert "[lld-macho] Avoid using bump-alloc in TrieBuider"
Sterling-Augustine Mar 14, 2022
07ac266
Reland "[lld-macho] Avoid using bump-alloc in TrieBuider""
oontvoo Mar 14, 2022
624ddd2
[lld-macho][nfc] Allow Defined symbols to be placed in binding sections
int3 Mar 15, 2022
b9caf0b
[lld-macho] -flat_namespace for dylibs should make all externs interp…
int3 Mar 15, 2022
75678eb
[lld-macho] Set FinalDefinitionInLinkageUnit on most LTO externs
int3 Mar 16, 2022
05a024c
[lld-macho][nfc] Have findContainingSubsection take a Section
int3 Mar 16, 2022
b3ffadc
[lld-macho][nfc] Add comment explaining why a cast<> is safe
int3 Mar 16, 2022
02652db
[lld-macho][nfc] Don't mix file sizes with addresses
int3 Sep 10, 2021
ad91602
[lld][Macho][NFC] Encapsulate priorities map in a priority class
Mar 23, 2022
f69beac
[lld/mac] Give range extension thunks for local symbols local visibility
nico Mar 29, 2022
65608cb
[lld-macho][NFC] Encapsulate symbol priority implementation.
Mar 31, 2022
8077780
Add output filename to UUID hash
speednoisemovement Mar 31, 2022
0a79be8
[lld/mac] Tweak a few comments
nico Apr 1, 2022
fa995de
[lld-macho][nfc] Rearrange order of statements to clarify data depend…
int3 Apr 7, 2022
d8158cf
Fix "result of 32-bit shift implicitly converted to 64 bits" MSVC war…
RKSimon Apr 7, 2022
99187d0
[lld/mac] Don't emit stabs entries for functions folded during ICF
nico Apr 6, 2022
e2014d6
[lld-macho][nfc] Factor out findSymbolAtOffset
int3 Apr 7, 2022
40b45ed
[lld-macho][nfc] Remove indirection when looking up common section me…
int3 Apr 7, 2022
8cd5a5a
[lld/mac] Add some comments and asserts
nico Apr 7, 2022
7276c44
[lld-macho][nfc] Give non-text ConcatOutputSections order-independent…
int3 Apr 7, 2022
70acf4c
Fix format specifier. NFCI.
slackito Apr 7, 2022
59f0d31
[lld-macho] Use fewer indirections in UnwindInfo implementation
int3 Apr 9, 2022
5108940
[lld-macho][nfc] Use includeInSymtab for all symtab-skipping logic
int3 Apr 11, 2022
957f303
[lld-macho][nfc] De-templatize UnwindInfoSection
int3 Apr 13, 2022
b4548d1
[lld/mac] Warn that writing zippered outputs isn't implemented
nico Apr 20, 2022
5131993
[lld-macho] Fix crash on invalid framework tbd
keith Apr 22, 2022
7b72579
[lld-macho][nfc] Simplify unwind section lookup
int3 Apr 22, 2022
cc44dad
[lld-macho] Fix ICF crash when comparing symbol relocs
int3 Apr 22, 2022
ab8c919
[lld-macho] Allow dead_strip to work with exported private extern sym…
thevinster Apr 21, 2022
3174660
[lld-macho] Remove stray debug printf
int3 Apr 23, 2022
3342245
[lld/mac] For catalyst outputs, tolerate implicitly linking against m…
nico Apr 22, 2022
230a86b
[lld/mac] Revert libcompiler_rt.dylib version check change
nico Apr 25, 2022
b3d02d5
[lld] Implement safe icf for MachO
alx32 May 4, 2022
bcdc9ca
[lld/mac] Support writing zippered dylibs and bundles
nico Apr 22, 2022
4b32a85
[lld/macho] Fixes the -ObjC flag
tapthaker May 11, 2022
5cd54ae
[lld-macho] Demangle symbol names in export-symbol error messages whe…
oontvoo May 16, 2022
e921d99
[lld-macho] Stop crash when emitting personalities with -dead_strip
abrachet May 20, 2022
7fef08a
[lld-macho][nfc] Run clang-format on lld/MachO/*.{h,cpp}
oontvoo May 24, 2022
03107c7
[lld-macho] Support -non_global_symbols_strip_list, -non_global_symb…
oontvoo May 20, 2022
3259772
[lld/mac] Cache file IDs of symbols in emitStabs for faster sorting
michaeleisel Jun 1, 2022
b4f990a
[lld/mac] clang-format after f5709066e3b
nico Jun 1, 2022
26d51cf
[lld-macho] Addressed additional post-commit comments from D126046
oontvoo Jun 1, 2022
73b61d3
[lld-macho] Demangle symbol names in duplicate-symbol error when -dem…
oontvoo Jun 3, 2022
55983b9
[lld/mac] Write output sections in parallel
michaeleisel Jun 9, 2022
96a20f7
[lld-macho] Add support for -w
keith Jun 11, 2022
033092e
[lld-macho] Make `--icf=safe` work with LTO
int3 Jun 12, 2022
37e5ac5
[lld-macho][reland] Initial support for EH Frames
int3 Jun 13, 2022
0cb9690
[lld-macho][reland] Support EH frames under arm64
int3 Jun 13, 2022
352dfdb
[ADT] Have ArrayRef::copy() return a MutableArrayRef
int3 Feb 18, 2022
1ce2aec
[lld-macho] Enable EH frame relocation / pruning
int3 Jul 14, 2022
f49c853
[MachO] Fix dead-stripping __eh_frame
smeenai Aug 22, 2022
4d771b2
[lld-macho] Support EH frame pointer encodings that use sdata4
int3 Aug 1, 2022
c082b51
[lld-macho] Fix assertion when two symbols at same addr have unwind info
int3 Jul 21, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[lld] Implement safe icf for MachO
This change implements --icf=safe for MachO based on addrsig section that is implemented in D123751.

Reviewed By: int3, #lld-macho

Differential Revision: https://reviews.llvm.org/D123752
  • Loading branch information
alx32 authored and gbaraldi committed Jan 6, 2023
commit b3d02d518a039a370f651b241c210ad069cdcc94
8 changes: 4 additions & 4 deletions lld/MachO/Driver.cpp
Original file line number Diff line number Diff line change
@@ -721,9 +721,6 @@ static ICFLevel getICFLevel(const ArgList &args) {
warn(Twine("unknown --icf=OPTION `") + icfLevelStr +
"', defaulting to `none'");
icfLevel = ICFLevel::none;
} else if (icfLevel == ICFLevel::safe) {
warn(Twine("`--icf=safe' is not yet implemented, reverting to `none'"));
icfLevel = ICFLevel::none;
}
return icfLevel;
}
@@ -1548,8 +1545,11 @@ bool macho::link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
// ICF assumes that all literals have been folded already, so we must run
// foldIdenticalLiterals before foldIdenticalSections.
foldIdenticalLiterals();
if (config->icfLevel != ICFLevel::none)
if (config->icfLevel != ICFLevel::none) {
if (config->icfLevel == ICFLevel::safe)
markAddrSigSymbols();
foldIdenticalSections();
}

// Write to an output file.
if (target->wordSize == 8)
38 changes: 37 additions & 1 deletion lld/MachO/ICF.cpp
Original file line number Diff line number Diff line change
@@ -8,11 +8,14 @@

#include "ICF.h"
#include "ConcatOutputSection.h"
#include "Config.h"
#include "InputSection.h"
#include "SymbolTable.h"
#include "Symbols.h"
#include "UnwindInfoSection.h"

#include "lld/Common/CommonLinkerContext.h"
#include "llvm/Support/LEB128.h"
#include "llvm/Support/Parallel.h"
#include "llvm/Support/TimeProfiler.h"
#include "llvm/Support/xxhash.h"
@@ -363,6 +366,39 @@ void ICF::segregate(size_t begin, size_t end, EqualsFn equals) {
}
}

void macho::markSymAsAddrSig(Symbol *s) {
if (auto *d = dyn_cast_or_null<Defined>(s))
if (d->isec)
d->isec->keepUnique = true;
}

void macho::markAddrSigSymbols() {
for (InputFile *file : inputFiles) {
ObjFile *obj = dyn_cast<ObjFile>(file);
if (!obj)
continue;

Section *addrSigSection = obj->addrSigSection;
if (!addrSigSection)
continue;
assert(addrSigSection->subsections.size() == 1);

Subsection *subSection = &addrSigSection->subsections[0];
ArrayRef<unsigned char> &contents = subSection->isec->data;

const uint8_t *pData = contents.begin();
while (pData != contents.end()) {
unsigned size;
const char *err;
uint32_t symIndex = decodeULEB128(pData, &size, contents.end(), &err);
if (err)
fatal(toString(file) + ": could not decode addrsig section: " + err);
markSymAsAddrSig(obj->symbols[symIndex]);
pData += size;
}
}
}

void macho::foldIdenticalSections() {
TimeTraceScope timeScope("Fold Identical Code Sections");
// The ICF equivalence-class segregation algorithm relies on pre-computed
@@ -385,7 +421,7 @@ void macho::foldIdenticalSections() {
// FIXME: consider non-code __text sections as hashable?
bool isHashable = (isCodeSection(isec) || isCfStringSection(isec) ||
isClassRefsSection(isec)) &&
!isec->shouldOmitFromOutput() &&
!isec->keepUnique && !isec->shouldOmitFromOutput() &&
sectionType(isec->getFlags()) == MachO::S_REGULAR;
if (isHashable) {
hashable.push_back(isec);
4 changes: 4 additions & 0 deletions lld/MachO/ICF.h
Original file line number Diff line number Diff line change
@@ -9,12 +9,16 @@
#ifndef LLD_MACHO_ICF_H
#define LLD_MACHO_ICF_H

#include "InputFiles.h"
#include "lld/Common/LLVM.h"
#include <vector>

namespace lld {
namespace macho {
class Symbol;

void markAddrSigSymbols();
void markSymAsAddrSig(Symbol *s);
void foldIdenticalSections();

} // namespace macho
3 changes: 3 additions & 0 deletions lld/MachO/InputFiles.cpp
Original file line number Diff line number Diff line change
@@ -354,6 +354,9 @@ void ObjFile::parseSections(ArrayRef<SectionHeader> sectionHeaders) {
// spurious duplicate symbol errors, we do not parse these sections.
// TODO: Evaluate whether the bitcode metadata is needed.
} else {
if (name == section_names::addrSig)
addrSigSection = sections.back();

auto *isec = make<ConcatInputSection>(section, data, align);
if (isDebugSection(isec->getFlags()) &&
isec->getSegName() == segment_names::dwarf) {
1 change: 1 addition & 0 deletions lld/MachO/InputFiles.h
Original file line number Diff line number Diff line change
@@ -149,6 +149,7 @@ class ObjFile final : public InputFile {
const uint32_t modTime;
std::vector<ConcatInputSection *> debugSections;
std::vector<CallGraphEntry> callGraph;
Section *addrSigSection = nullptr;

private:
template <class LP> void parseLazy();
3 changes: 3 additions & 0 deletions lld/MachO/InputSection.h
Original file line number Diff line number Diff line change
@@ -71,6 +71,8 @@ class InputSection {
public:
// is address assigned?
bool isFinal = false;
// keep the address of the symbol(s) in this section unique in the final binary ?
bool keepUnique = false;
uint32_t align = 1;

OutputSection *parent = nullptr;
@@ -328,6 +330,7 @@ constexpr const char threadVars[] = "__thread_vars";
constexpr const char unwindInfo[] = "__unwind_info";
constexpr const char weakBinding[] = "__weak_binding";
constexpr const char zeroFill[] = "__zerofill";
constexpr const char addrSig[] = "__llvm_addrsig";

} // namespace section_names

5 changes: 2 additions & 3 deletions lld/test/MachO/icf-options.s
Original file line number Diff line number Diff line change
@@ -8,8 +8,8 @@
# RUN: | FileCheck %s --check-prefix=DIAG-EMPTY --allow-empty
# RUN: %lld -lSystem -no_deduplicate -o %t/no_dedup %t/main.o 2>&1 \
# RUN: | FileCheck %s --check-prefix=DIAG-EMPTY --allow-empty
# RUN: not %lld -lSystem --icf=safe -o %t/safe %t/main.o 2>&1 \
# RUN: | FileCheck %s --check-prefix=DIAG-SAFE
# RUN: %lld -lSystem --icf=safe -o %t/safe %t/main.o 2>&1 \
# RUN: | FileCheck %s --check-prefix=DIAG-EMPTY --allow-empty
# RUN: not %lld -lSystem --icf=junk -o %t/junk %t/main.o 2>&1 \
# RUN: | FileCheck %s --check-prefix=DIAG-JUNK
# RUN: %lld -lSystem --icf=all -no_deduplicate -o %t/none2 %t/main.o 2>&1 \
@@ -18,7 +18,6 @@
# RUN: | FileCheck %s --check-prefix=DIAG-EMPTY --allow-empty

# DIAG-EMPTY-NOT: {{.}}
# DIAG-SAFE: `--icf=safe' is not yet implemented, reverting to `none'
# DIAG-JUNK: unknown --icf=OPTION `junk', defaulting to `none'

# RUN: llvm-objdump -d --syms %t/all | FileCheck %s --check-prefix=FOLD
56 changes: 56 additions & 0 deletions lld/test/MachO/icf-safe.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
; RUN: rm -rf %t; mkdir %t
; RUN: llc -filetype=obj %s -O3 -o %t/icf-obj.o -enable-machine-outliner=never -mtriple arm64-apple-ios -addrsig
; RUN: %lld -arch arm64 -lSystem --icf=safe -dylib -o %t/icf-safe.dylib %t/icf-obj.o
; RUN: %lld -arch arm64 -lSystem --icf=all -dylib -o %t/icf-all.dylib %t/icf-obj.o
; RUN: llvm-objdump %t/icf-safe.dylib -d --macho | FileCheck %s --check-prefix=ICFSAFE
; RUN: llvm-objdump %t/icf-all.dylib -d --macho | FileCheck %s --check-prefix=ICFALL

; ICFSAFE-LABEL: _callAllFunctions
; ICFSAFE: bl _func02
; ICFSAFE-NEXT: bl _func02
; ICFSAFE-NEXT: bl _func03_takeaddr

; ICFALL-LABEL: _callAllFunctions
; ICFALL: bl _func03_takeaddr
; ICFALL-NEXT: bl _func03_takeaddr
; ICFALL-NEXT: bl _func03_takeaddr

target datalayout = "e-m:o-i64:64-i128:128-n32:64-S128"
target triple = "arm64-apple-ios7.0.0"

@result = global i32 0, align 4

define void @func01() local_unnamed_addr noinline {
entry:
%0 = load volatile i32, ptr @result, align 4
%add = add nsw i32 %0, 1
store volatile i32 %add, ptr @result, align 4
ret void
}

define void @func02() local_unnamed_addr noinline {
entry:
%0 = load volatile i32, ptr @result, align 4
%add = add nsw i32 %0, 1
store volatile i32 %add, ptr @result, align 4
ret void
}

define void @func03_takeaddr() noinline {
entry:
%0 = load volatile i32, ptr @result, align 4
%add = add nsw i32 %0, 1
store volatile i32 %add, ptr @result, align 4
ret void
}

define void @callAllFunctions() local_unnamed_addr {
entry:
tail call void @func01()
tail call void @func02()
tail call void @func03_takeaddr()
%0 = load volatile i32, ptr @result, align 4
%add = add nsw i32 %0, ptrtoint (ptr @func03_takeaddr to i32)
store volatile i32 %add, ptr @result, align 4
ret void
}