[lld-macho][reland] Add basic symbol table output
This diff implements basic support for writing a symbol table.
Attributes are loosely supported for extern symbols and not at all for
other types.
Initial version by Kellie Medlin <kelliem@fb.com>
Originally committed in a3d95a50ee33 and reverted in fbae153ca583 due to
UBSAN erroring over unaligned writes. That has been fixed in the
current diff with the following changes:
```
diff --git a/lld/MachO/SyntheticSections.cpp b/lld/MachO/SyntheticSections.cpp
--- a/lld/MachO/SyntheticSections.cpp
+++ b/lld/MachO/SyntheticSections.cpp
@@ -133,6 +133,9 @@ SymtabSection::SymtabSection(StringTableSection &stringTableSection)
: stringTableSection(stringTableSection) {
segname = segment_names::linkEdit;
name = section_names::symbolTable;
+ // TODO: When we introduce the SyntheticSections superclass, we should make
+ // all synthetic sections aligned to WordSize by default.
+ align = WordSize;
}
size_t SymtabSection::getSize() const {
diff --git a/lld/MachO/Writer.cpp b/lld/MachO/Writer.cpp
--- a/lld/MachO/Writer.cpp
+++ b/lld/MachO/Writer.cpp
@@ -371,6 +371,7 @@ void Writer::assignAddresses(OutputSegment *seg) {
ArrayRef<InputSection *> sections = p.second;
for (InputSection *isec : sections) {
addr = alignTo(addr, isec->align);
+ // We must align the file offsets too to avoid misaligned writes of
+ // structs.
+ fileOff = alignTo(fileOff, isec->align);
isec->addr = addr;
addr += isec->getSize();
fileOff += isec->getFileSize();
@@ -396,6 +397,7 @@ void Writer::writeSections() {
uint64_t fileOff = seg->fileOff;
for (auto § : seg->getSections()) {
for (InputSection *isec : sect.second) {
+ fileOff = alignTo(fileOff, isec->align);
isec->writeTo(buf + fileOff);
fileOff += isec->getFileSize();
}
```
I don't think it's easy to write a test for alignment (that doesn't
involve brittly hard-coding file offsets), so there isn't one... but
UBSAN builds pass now.
Differential Revision: https://reviews.llvm.org/D79050
2020-04-29 07:58:19 +08:00
|
|
|
# REQUIRES: x86
|
2020-12-02 06:45:09 +08:00
|
|
|
# RUN: rm -rf %t && split-file %s %t
|
|
|
|
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/test.s -o %t/test.o
|
2021-05-18 01:49:17 +08:00
|
|
|
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/test2.s -o %t/test2.o
|
2020-12-02 06:45:09 +08:00
|
|
|
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/libfoo.s -o %t/libfoo.o
|
|
|
|
# RUN: %lld -dylib %t/libfoo.o -o %t/libfoo.dylib
|
2021-05-18 01:49:17 +08:00
|
|
|
# RUN: %lld -lSystem %t/test.o %t/test2.o %t/libfoo.dylib -o %t/test
|
[lld-macho][reland] Add basic symbol table output
This diff implements basic support for writing a symbol table.
Attributes are loosely supported for extern symbols and not at all for
other types.
Initial version by Kellie Medlin <kelliem@fb.com>
Originally committed in a3d95a50ee33 and reverted in fbae153ca583 due to
UBSAN erroring over unaligned writes. That has been fixed in the
current diff with the following changes:
```
diff --git a/lld/MachO/SyntheticSections.cpp b/lld/MachO/SyntheticSections.cpp
--- a/lld/MachO/SyntheticSections.cpp
+++ b/lld/MachO/SyntheticSections.cpp
@@ -133,6 +133,9 @@ SymtabSection::SymtabSection(StringTableSection &stringTableSection)
: stringTableSection(stringTableSection) {
segname = segment_names::linkEdit;
name = section_names::symbolTable;
+ // TODO: When we introduce the SyntheticSections superclass, we should make
+ // all synthetic sections aligned to WordSize by default.
+ align = WordSize;
}
size_t SymtabSection::getSize() const {
diff --git a/lld/MachO/Writer.cpp b/lld/MachO/Writer.cpp
--- a/lld/MachO/Writer.cpp
+++ b/lld/MachO/Writer.cpp
@@ -371,6 +371,7 @@ void Writer::assignAddresses(OutputSegment *seg) {
ArrayRef<InputSection *> sections = p.second;
for (InputSection *isec : sections) {
addr = alignTo(addr, isec->align);
+ // We must align the file offsets too to avoid misaligned writes of
+ // structs.
+ fileOff = alignTo(fileOff, isec->align);
isec->addr = addr;
addr += isec->getSize();
fileOff += isec->getFileSize();
@@ -396,6 +397,7 @@ void Writer::writeSections() {
uint64_t fileOff = seg->fileOff;
for (auto § : seg->getSections()) {
for (InputSection *isec : sect.second) {
+ fileOff = alignTo(fileOff, isec->align);
isec->writeTo(buf + fileOff);
fileOff += isec->getFileSize();
}
```
I don't think it's easy to write a test for alignment (that doesn't
involve brittly hard-coding file offsets), so there isn't one... but
UBSAN builds pass now.
Differential Revision: https://reviews.llvm.org/D79050
2020-04-29 07:58:19 +08:00
|
|
|
|
2022-04-11 23:58:31 +08:00
|
|
|
# RUN: llvm-readobj --syms --sort-symbols=type,name --macho-dysymtab %t/test | FileCheck %s
|
[lld-macho][reland] Add basic symbol table output
This diff implements basic support for writing a symbol table.
Attributes are loosely supported for extern symbols and not at all for
other types.
Initial version by Kellie Medlin <kelliem@fb.com>
Originally committed in a3d95a50ee33 and reverted in fbae153ca583 due to
UBSAN erroring over unaligned writes. That has been fixed in the
current diff with the following changes:
```
diff --git a/lld/MachO/SyntheticSections.cpp b/lld/MachO/SyntheticSections.cpp
--- a/lld/MachO/SyntheticSections.cpp
+++ b/lld/MachO/SyntheticSections.cpp
@@ -133,6 +133,9 @@ SymtabSection::SymtabSection(StringTableSection &stringTableSection)
: stringTableSection(stringTableSection) {
segname = segment_names::linkEdit;
name = section_names::symbolTable;
+ // TODO: When we introduce the SyntheticSections superclass, we should make
+ // all synthetic sections aligned to WordSize by default.
+ align = WordSize;
}
size_t SymtabSection::getSize() const {
diff --git a/lld/MachO/Writer.cpp b/lld/MachO/Writer.cpp
--- a/lld/MachO/Writer.cpp
+++ b/lld/MachO/Writer.cpp
@@ -371,6 +371,7 @@ void Writer::assignAddresses(OutputSegment *seg) {
ArrayRef<InputSection *> sections = p.second;
for (InputSection *isec : sections) {
addr = alignTo(addr, isec->align);
+ // We must align the file offsets too to avoid misaligned writes of
+ // structs.
+ fileOff = alignTo(fileOff, isec->align);
isec->addr = addr;
addr += isec->getSize();
fileOff += isec->getFileSize();
@@ -396,6 +397,7 @@ void Writer::writeSections() {
uint64_t fileOff = seg->fileOff;
for (auto § : seg->getSections()) {
for (InputSection *isec : sect.second) {
+ fileOff = alignTo(fileOff, isec->align);
isec->writeTo(buf + fileOff);
fileOff += isec->getFileSize();
}
```
I don't think it's easy to write a test for alignment (that doesn't
involve brittly hard-coding file offsets), so there isn't one... but
UBSAN builds pass now.
Differential Revision: https://reviews.llvm.org/D79050
2020-04-29 07:58:19 +08:00
|
|
|
# CHECK: Symbols [
|
|
|
|
# CHECK-NEXT: Symbol {
|
2022-04-11 23:58:31 +08:00
|
|
|
# CHECK-NEXT: Name: _dynamic
|
|
|
|
# CHECK-NEXT: Extern
|
|
|
|
# CHECK-NEXT: Type: Undef (0x0)
|
|
|
|
# CHECK-NEXT: Section: (0x0)
|
2020-12-02 06:45:09 +08:00
|
|
|
# CHECK-NEXT: RefType: UndefinedNonLazy (0x0)
|
2022-04-11 23:58:31 +08:00
|
|
|
# CHECK-NEXT: Flags [ (0x200)
|
|
|
|
# CHECK-NEXT: AltEntry (0x200)
|
2020-12-02 06:45:09 +08:00
|
|
|
# CHECK-NEXT: ]
|
2022-04-11 23:58:31 +08:00
|
|
|
# CHECK-NEXT: Value: 0x0
|
|
|
|
# CHECK-NEXT: }
|
|
|
|
# CHECK-NEXT: Symbol {
|
|
|
|
# CHECK-NEXT: Name: dyld_stub_binder
|
|
|
|
# CHECK-NEXT: Extern
|
|
|
|
# CHECK-NEXT: Type: Undef (0x0)
|
|
|
|
# CHECK-NEXT: Section: (0x0)
|
|
|
|
# CHECK-NEXT: RefType: UndefinedNonLazy (0x0)
|
|
|
|
# CHECK-NEXT: Flags [ (0x100)
|
|
|
|
# CHECK-NEXT: SymbolResolver (0x100)
|
|
|
|
# CHECK-NEXT: ]
|
|
|
|
# CHECK-NEXT: Value: 0x0
|
2020-12-02 06:45:09 +08:00
|
|
|
# CHECK-NEXT: }
|
|
|
|
# CHECK-NEXT: Symbol {
|
[lld/mac] Implement support for private extern symbols
Private extern symbols are used for things scoped to the linkage unit.
They cause duplicate symbol errors (so they're in the symbol table,
unlike TU-scoped truly local symbols), but they don't make it into the
export trie. They are created e.g. by compiling with
-fvisibility=hidden.
If two weak symbols have differing privateness, the combined symbol is
non-private external. (Example: inline functions and some TUs that
include the header defining it were built with
-fvisibility-inlines-hidden and some weren't).
A weak private external symbol implicitly has its "weak" dropped and
behaves like a regular strong private external symbol: Weak is an export
trie concept, and private symbols are not in the export trie.
If a weak and a strong symbol have different privateness, the strong
symbol wins.
If two common symbols have differing privateness, the larger symbol
wins. If they have the same size, the privateness of the symbol seen
later during the link wins (!) -- this is a bit lame, but it matches
ld64 and this behavior takes 2 lines less to implement than the less
surprising "result is non-private external), so match ld64.
(Example: `int a` in two .c files, both built with -fcommon,
one built with -fvisibility=hidden and one without.)
This also makes `__dyld_private` a true TU-local symbol, matching ld64.
To make this work, make the `const char*` StringRefZ ctor to correctly
set `size` (without this, writing the string table crashed when calling
getName() on the __dyld_private symbol).
Mention in CommonSymbol's comment that common symbols are now disabled
by default in clang.
Mention in -keep_private_externs's HelpText that the flag only has an
effect with `-r` (which we don't implement yet -- so this patch here
doesn't regress any behavior around -r + -keep_private_externs)). ld64
doesn't explicitly document it, but the commit text of
http://reviews.llvm.org/rL216146 does, and ld64's
OutputFile::buildSymbolTable() checks `_options.outputKind() ==
Options::kObjectFile` before calling `_options.keepPrivateExterns()`
(the only reference to that function).
Fixes PR48536.
Differential Revision: https://reviews.llvm.org/D93609
2020-12-18 02:30:18 +08:00
|
|
|
# CHECK-NEXT: Name: __dyld_private
|
[lld-macho][reland] Add basic symbol table output
This diff implements basic support for writing a symbol table.
Attributes are loosely supported for extern symbols and not at all for
other types.
Initial version by Kellie Medlin <kelliem@fb.com>
Originally committed in a3d95a50ee33 and reverted in fbae153ca583 due to
UBSAN erroring over unaligned writes. That has been fixed in the
current diff with the following changes:
```
diff --git a/lld/MachO/SyntheticSections.cpp b/lld/MachO/SyntheticSections.cpp
--- a/lld/MachO/SyntheticSections.cpp
+++ b/lld/MachO/SyntheticSections.cpp
@@ -133,6 +133,9 @@ SymtabSection::SymtabSection(StringTableSection &stringTableSection)
: stringTableSection(stringTableSection) {
segname = segment_names::linkEdit;
name = section_names::symbolTable;
+ // TODO: When we introduce the SyntheticSections superclass, we should make
+ // all synthetic sections aligned to WordSize by default.
+ align = WordSize;
}
size_t SymtabSection::getSize() const {
diff --git a/lld/MachO/Writer.cpp b/lld/MachO/Writer.cpp
--- a/lld/MachO/Writer.cpp
+++ b/lld/MachO/Writer.cpp
@@ -371,6 +371,7 @@ void Writer::assignAddresses(OutputSegment *seg) {
ArrayRef<InputSection *> sections = p.second;
for (InputSection *isec : sections) {
addr = alignTo(addr, isec->align);
+ // We must align the file offsets too to avoid misaligned writes of
+ // structs.
+ fileOff = alignTo(fileOff, isec->align);
isec->addr = addr;
addr += isec->getSize();
fileOff += isec->getFileSize();
@@ -396,6 +397,7 @@ void Writer::writeSections() {
uint64_t fileOff = seg->fileOff;
for (auto § : seg->getSections()) {
for (InputSection *isec : sect.second) {
+ fileOff = alignTo(fileOff, isec->align);
isec->writeTo(buf + fileOff);
fileOff += isec->getFileSize();
}
```
I don't think it's easy to write a test for alignment (that doesn't
involve brittly hard-coding file offsets), so there isn't one... but
UBSAN builds pass now.
Differential Revision: https://reviews.llvm.org/D79050
2020-04-29 07:58:19 +08:00
|
|
|
# CHECK-NEXT: Type: Section (0xE)
|
2021-01-03 02:31:55 +08:00
|
|
|
# CHECK-NEXT: Section: __data
|
2020-12-02 06:45:09 +08:00
|
|
|
# CHECK-NEXT: RefType: UndefinedNonLazy (0x0)
|
|
|
|
# CHECK-NEXT: Flags [ (0x0)
|
|
|
|
# CHECK-NEXT: ]
|
|
|
|
# CHECK-NEXT: Value: 0x1{{[0-9a-f]*}}
|
|
|
|
# CHECK-NEXT: }
|
|
|
|
# CHECK-NEXT: Symbol {
|
2022-04-11 23:58:31 +08:00
|
|
|
# CHECK-NEXT: Name: _local
|
2021-05-18 01:49:17 +08:00
|
|
|
# CHECK-NEXT: Type: Section (0xE)
|
2022-04-11 23:58:31 +08:00
|
|
|
# CHECK-NEXT: Section: __data
|
2021-05-18 01:49:17 +08:00
|
|
|
# CHECK-NEXT: RefType: UndefinedNonLazy (0x0)
|
|
|
|
# CHECK-NEXT: Flags [ (0x0)
|
|
|
|
# CHECK-NEXT: ]
|
|
|
|
# CHECK-NEXT: Value: 0x1{{[0-9a-f]*}}
|
|
|
|
# CHECK-NEXT: }
|
|
|
|
# CHECK-NEXT: Symbol {
|
2022-04-11 23:58:31 +08:00
|
|
|
# CHECK-NEXT: Name: __mh_execute_header
|
|
|
|
# CHECK-NEXT: Extern
|
2021-05-18 01:49:17 +08:00
|
|
|
# CHECK-NEXT: Type: Section (0xE)
|
|
|
|
# CHECK-NEXT: Section: __text (0x1)
|
|
|
|
# CHECK-NEXT: RefType: UndefinedNonLazy (0x0)
|
2022-04-11 23:58:31 +08:00
|
|
|
# CHECK-NEXT: Flags [ (0x10)
|
|
|
|
# CHECK-NEXT: ReferencedDynamically (0x10)
|
2021-05-18 01:49:17 +08:00
|
|
|
# CHECK-NEXT: ]
|
2022-04-11 23:58:31 +08:00
|
|
|
# CHECK-NEXT: Value: 0x100000000
|
2021-05-18 01:49:17 +08:00
|
|
|
# CHECK-NEXT: }
|
|
|
|
# CHECK-NEXT: Symbol {
|
2022-04-11 23:58:31 +08:00
|
|
|
# CHECK-NEXT: Name: _external
|
2020-12-02 06:45:09 +08:00
|
|
|
# CHECK-NEXT: Extern
|
|
|
|
# CHECK-NEXT: Type: Section (0xE)
|
2022-04-11 23:58:31 +08:00
|
|
|
# CHECK-NEXT: Section: __data
|
2020-12-02 06:45:09 +08:00
|
|
|
# CHECK-NEXT: RefType: UndefinedNonLazy (0x0)
|
[lld-macho][reland] Add basic symbol table output
This diff implements basic support for writing a symbol table.
Attributes are loosely supported for extern symbols and not at all for
other types.
Initial version by Kellie Medlin <kelliem@fb.com>
Originally committed in a3d95a50ee33 and reverted in fbae153ca583 due to
UBSAN erroring over unaligned writes. That has been fixed in the
current diff with the following changes:
```
diff --git a/lld/MachO/SyntheticSections.cpp b/lld/MachO/SyntheticSections.cpp
--- a/lld/MachO/SyntheticSections.cpp
+++ b/lld/MachO/SyntheticSections.cpp
@@ -133,6 +133,9 @@ SymtabSection::SymtabSection(StringTableSection &stringTableSection)
: stringTableSection(stringTableSection) {
segname = segment_names::linkEdit;
name = section_names::symbolTable;
+ // TODO: When we introduce the SyntheticSections superclass, we should make
+ // all synthetic sections aligned to WordSize by default.
+ align = WordSize;
}
size_t SymtabSection::getSize() const {
diff --git a/lld/MachO/Writer.cpp b/lld/MachO/Writer.cpp
--- a/lld/MachO/Writer.cpp
+++ b/lld/MachO/Writer.cpp
@@ -371,6 +371,7 @@ void Writer::assignAddresses(OutputSegment *seg) {
ArrayRef<InputSection *> sections = p.second;
for (InputSection *isec : sections) {
addr = alignTo(addr, isec->align);
+ // We must align the file offsets too to avoid misaligned writes of
+ // structs.
+ fileOff = alignTo(fileOff, isec->align);
isec->addr = addr;
addr += isec->getSize();
fileOff += isec->getFileSize();
@@ -396,6 +397,7 @@ void Writer::writeSections() {
uint64_t fileOff = seg->fileOff;
for (auto § : seg->getSections()) {
for (InputSection *isec : sect.second) {
+ fileOff = alignTo(fileOff, isec->align);
isec->writeTo(buf + fileOff);
fileOff += isec->getFileSize();
}
```
I don't think it's easy to write a test for alignment (that doesn't
involve brittly hard-coding file offsets), so there isn't one... but
UBSAN builds pass now.
Differential Revision: https://reviews.llvm.org/D79050
2020-04-29 07:58:19 +08:00
|
|
|
# CHECK-NEXT: Flags [ (0x0)
|
|
|
|
# CHECK-NEXT: ]
|
2020-12-02 06:45:09 +08:00
|
|
|
# CHECK-NEXT: Value: 0x1{{[0-9a-f]*}}
|
[lld-macho][reland] Add basic symbol table output
This diff implements basic support for writing a symbol table.
Attributes are loosely supported for extern symbols and not at all for
other types.
Initial version by Kellie Medlin <kelliem@fb.com>
Originally committed in a3d95a50ee33 and reverted in fbae153ca583 due to
UBSAN erroring over unaligned writes. That has been fixed in the
current diff with the following changes:
```
diff --git a/lld/MachO/SyntheticSections.cpp b/lld/MachO/SyntheticSections.cpp
--- a/lld/MachO/SyntheticSections.cpp
+++ b/lld/MachO/SyntheticSections.cpp
@@ -133,6 +133,9 @@ SymtabSection::SymtabSection(StringTableSection &stringTableSection)
: stringTableSection(stringTableSection) {
segname = segment_names::linkEdit;
name = section_names::symbolTable;
+ // TODO: When we introduce the SyntheticSections superclass, we should make
+ // all synthetic sections aligned to WordSize by default.
+ align = WordSize;
}
size_t SymtabSection::getSize() const {
diff --git a/lld/MachO/Writer.cpp b/lld/MachO/Writer.cpp
--- a/lld/MachO/Writer.cpp
+++ b/lld/MachO/Writer.cpp
@@ -371,6 +371,7 @@ void Writer::assignAddresses(OutputSegment *seg) {
ArrayRef<InputSection *> sections = p.second;
for (InputSection *isec : sections) {
addr = alignTo(addr, isec->align);
+ // We must align the file offsets too to avoid misaligned writes of
+ // structs.
+ fileOff = alignTo(fileOff, isec->align);
isec->addr = addr;
addr += isec->getSize();
fileOff += isec->getFileSize();
@@ -396,6 +397,7 @@ void Writer::writeSections() {
uint64_t fileOff = seg->fileOff;
for (auto § : seg->getSections()) {
for (InputSection *isec : sect.second) {
+ fileOff = alignTo(fileOff, isec->align);
isec->writeTo(buf + fileOff);
fileOff += isec->getFileSize();
}
```
I don't think it's easy to write a test for alignment (that doesn't
involve brittly hard-coding file offsets), so there isn't one... but
UBSAN builds pass now.
Differential Revision: https://reviews.llvm.org/D79050
2020-04-29 07:58:19 +08:00
|
|
|
# CHECK-NEXT: }
|
2020-04-30 06:42:19 +08:00
|
|
|
# CHECK-NEXT: Symbol {
|
2021-04-07 03:09:14 +08:00
|
|
|
# CHECK-NEXT: Name: _external_weak
|
2020-04-30 06:42:19 +08:00
|
|
|
# CHECK-NEXT: Extern
|
|
|
|
# CHECK-NEXT: Type: Section (0xE)
|
2021-04-07 03:09:14 +08:00
|
|
|
# CHECK-NEXT: Section: __text (0x1)
|
2020-12-02 06:45:09 +08:00
|
|
|
# CHECK-NEXT: RefType: UndefinedNonLazy (0x0)
|
2021-04-07 03:09:14 +08:00
|
|
|
# CHECK-NEXT: Flags [ (0x80)
|
|
|
|
# CHECK-NEXT: WeakDef (0x80)
|
2020-04-30 06:42:19 +08:00
|
|
|
# CHECK-NEXT: ]
|
2020-12-02 06:45:09 +08:00
|
|
|
# CHECK-NEXT: Value: 0x1{{[0-9a-f]*}}
|
2020-04-30 06:42:19 +08:00
|
|
|
# CHECK-NEXT: }
|
|
|
|
# CHECK-NEXT: Symbol {
|
2022-04-11 23:58:31 +08:00
|
|
|
# CHECK-NEXT: Name: _main
|
2020-04-30 06:42:19 +08:00
|
|
|
# CHECK-NEXT: Extern
|
|
|
|
# CHECK-NEXT: Type: Section (0xE)
|
2022-04-11 23:58:31 +08:00
|
|
|
# CHECK-NEXT: Section: __text (0x1)
|
2020-12-02 06:45:09 +08:00
|
|
|
# CHECK-NEXT: RefType: UndefinedNonLazy (0x0)
|
2021-04-07 03:09:14 +08:00
|
|
|
# CHECK-NEXT: Flags [ (0x0)
|
2020-04-30 06:42:19 +08:00
|
|
|
# CHECK-NEXT: ]
|
2020-12-02 06:45:09 +08:00
|
|
|
# CHECK-NEXT: Value: 0x1{{[0-9a-f]*}}
|
|
|
|
# CHECK-NEXT: }
|
2021-05-18 01:49:17 +08:00
|
|
|
# CHECK-NEXT: Symbol {
|
2022-04-11 23:58:31 +08:00
|
|
|
# CHECK-NEXT: Name: _private_external
|
|
|
|
# CHECK-NEXT: PrivateExtern
|
2021-05-18 01:49:17 +08:00
|
|
|
# CHECK-NEXT: Type: Section (0xE)
|
|
|
|
# CHECK-NEXT: Section: __text (0x1)
|
|
|
|
# CHECK-NEXT: RefType: UndefinedNonLazy (0x0)
|
2022-04-11 23:58:31 +08:00
|
|
|
# CHECK-NEXT: Flags [ (0x0)
|
2020-12-02 06:45:09 +08:00
|
|
|
# CHECK-NEXT: ]
|
2022-04-11 23:58:31 +08:00
|
|
|
# CHECK-NEXT: Value: 0x1{{[0-9a-f]*}}
|
2020-12-02 06:45:09 +08:00
|
|
|
# CHECK-NEXT: }
|
|
|
|
# CHECK-NEXT: Symbol {
|
2022-04-11 23:58:31 +08:00
|
|
|
# CHECK-NEXT: Name: _private_external_weak
|
|
|
|
# CHECK-NEXT: PrivateExtern
|
|
|
|
# CHECK-NEXT: Type: Section (0xE)
|
|
|
|
# CHECK-NEXT: Section: __text (0x1)
|
2020-12-02 06:45:09 +08:00
|
|
|
# CHECK-NEXT: RefType: UndefinedNonLazy (0x0)
|
2022-04-11 23:58:31 +08:00
|
|
|
# CHECK-NEXT: Flags [ (0x0)
|
2020-12-02 06:45:09 +08:00
|
|
|
# CHECK-NEXT: ]
|
2022-04-11 23:58:31 +08:00
|
|
|
# CHECK-NEXT: Value: 0x1{{[0-9a-f]*}}
|
2020-04-30 06:42:19 +08:00
|
|
|
# CHECK-NEXT: }
|
[lld-macho][reland] Add basic symbol table output
This diff implements basic support for writing a symbol table.
Attributes are loosely supported for extern symbols and not at all for
other types.
Initial version by Kellie Medlin <kelliem@fb.com>
Originally committed in a3d95a50ee33 and reverted in fbae153ca583 due to
UBSAN erroring over unaligned writes. That has been fixed in the
current diff with the following changes:
```
diff --git a/lld/MachO/SyntheticSections.cpp b/lld/MachO/SyntheticSections.cpp
--- a/lld/MachO/SyntheticSections.cpp
+++ b/lld/MachO/SyntheticSections.cpp
@@ -133,6 +133,9 @@ SymtabSection::SymtabSection(StringTableSection &stringTableSection)
: stringTableSection(stringTableSection) {
segname = segment_names::linkEdit;
name = section_names::symbolTable;
+ // TODO: When we introduce the SyntheticSections superclass, we should make
+ // all synthetic sections aligned to WordSize by default.
+ align = WordSize;
}
size_t SymtabSection::getSize() const {
diff --git a/lld/MachO/Writer.cpp b/lld/MachO/Writer.cpp
--- a/lld/MachO/Writer.cpp
+++ b/lld/MachO/Writer.cpp
@@ -371,6 +371,7 @@ void Writer::assignAddresses(OutputSegment *seg) {
ArrayRef<InputSection *> sections = p.second;
for (InputSection *isec : sections) {
addr = alignTo(addr, isec->align);
+ // We must align the file offsets too to avoid misaligned writes of
+ // structs.
+ fileOff = alignTo(fileOff, isec->align);
isec->addr = addr;
addr += isec->getSize();
fileOff += isec->getFileSize();
@@ -396,6 +397,7 @@ void Writer::writeSections() {
uint64_t fileOff = seg->fileOff;
for (auto § : seg->getSections()) {
for (InputSection *isec : sect.second) {
+ fileOff = alignTo(fileOff, isec->align);
isec->writeTo(buf + fileOff);
fileOff += isec->getFileSize();
}
```
I don't think it's easy to write a test for alignment (that doesn't
involve brittly hard-coding file offsets), so there isn't one... but
UBSAN builds pass now.
Differential Revision: https://reviews.llvm.org/D79050
2020-04-29 07:58:19 +08:00
|
|
|
# CHECK-NEXT: ]
|
2020-12-02 06:45:09 +08:00
|
|
|
# CHECK-NEXT: Dysymtab {
|
|
|
|
# CHECK-NEXT: ilocalsym: 0
|
2021-05-18 01:49:17 +08:00
|
|
|
# CHECK-NEXT: nlocalsym: 4
|
|
|
|
# CHECK-NEXT: iextdefsym: 4
|
2021-03-19 06:49:45 +08:00
|
|
|
# CHECK-NEXT: nextdefsym: 4
|
2021-05-18 01:49:17 +08:00
|
|
|
# CHECK-NEXT: iundefsym: 8
|
2020-12-02 06:45:09 +08:00
|
|
|
# CHECK-NEXT: nundefsym: 2
|
|
|
|
|
2020-12-23 00:00:57 +08:00
|
|
|
## Verify that the first entry in the StringTable is a space, and that
|
|
|
|
## unreferenced symbols aren't emitted.
|
2020-12-02 06:45:10 +08:00
|
|
|
# RUN: obj2yaml %t/test | FileCheck %s --check-prefix=YAML
|
|
|
|
# YAML: StringTable:
|
|
|
|
# YAML-NEXT: ' '
|
2020-12-23 00:00:57 +08:00
|
|
|
# YAML-NOT: _unreferenced
|
2020-12-02 06:45:10 +08:00
|
|
|
|
2020-12-02 06:45:09 +08:00
|
|
|
#--- libfoo.s
|
|
|
|
.globl _dynamic
|
|
|
|
_dynamic:
|
|
|
|
|
|
|
|
#--- test.s
|
2021-05-18 01:49:17 +08:00
|
|
|
.globl _main, _external, _private_external, _external_weak, _private_external_weak, _unreferenced
|
[lld-macho][reland] Add basic symbol table output
This diff implements basic support for writing a symbol table.
Attributes are loosely supported for extern symbols and not at all for
other types.
Initial version by Kellie Medlin <kelliem@fb.com>
Originally committed in a3d95a50ee33 and reverted in fbae153ca583 due to
UBSAN erroring over unaligned writes. That has been fixed in the
current diff with the following changes:
```
diff --git a/lld/MachO/SyntheticSections.cpp b/lld/MachO/SyntheticSections.cpp
--- a/lld/MachO/SyntheticSections.cpp
+++ b/lld/MachO/SyntheticSections.cpp
@@ -133,6 +133,9 @@ SymtabSection::SymtabSection(StringTableSection &stringTableSection)
: stringTableSection(stringTableSection) {
segname = segment_names::linkEdit;
name = section_names::symbolTable;
+ // TODO: When we introduce the SyntheticSections superclass, we should make
+ // all synthetic sections aligned to WordSize by default.
+ align = WordSize;
}
size_t SymtabSection::getSize() const {
diff --git a/lld/MachO/Writer.cpp b/lld/MachO/Writer.cpp
--- a/lld/MachO/Writer.cpp
+++ b/lld/MachO/Writer.cpp
@@ -371,6 +371,7 @@ void Writer::assignAddresses(OutputSegment *seg) {
ArrayRef<InputSection *> sections = p.second;
for (InputSection *isec : sections) {
addr = alignTo(addr, isec->align);
+ // We must align the file offsets too to avoid misaligned writes of
+ // structs.
+ fileOff = alignTo(fileOff, isec->align);
isec->addr = addr;
addr += isec->getSize();
fileOff += isec->getFileSize();
@@ -396,6 +397,7 @@ void Writer::writeSections() {
uint64_t fileOff = seg->fileOff;
for (auto § : seg->getSections()) {
for (InputSection *isec : sect.second) {
+ fileOff = alignTo(fileOff, isec->align);
isec->writeTo(buf + fileOff);
fileOff += isec->getFileSize();
}
```
I don't think it's easy to write a test for alignment (that doesn't
involve brittly hard-coding file offsets), so there isn't one... but
UBSAN builds pass now.
Differential Revision: https://reviews.llvm.org/D79050
2020-04-29 07:58:19 +08:00
|
|
|
|
2020-04-30 06:42:19 +08:00
|
|
|
.data
|
2020-12-02 06:45:09 +08:00
|
|
|
_external:
|
2021-05-18 01:49:17 +08:00
|
|
|
.space 1
|
2020-12-02 06:45:09 +08:00
|
|
|
_local:
|
2021-05-18 01:49:17 +08:00
|
|
|
.space 1
|
2020-04-30 06:42:19 +08:00
|
|
|
|
|
|
|
.text
|
2020-12-02 06:45:09 +08:00
|
|
|
.weak_definition _external_weak
|
|
|
|
_external_weak:
|
2021-05-18 01:49:17 +08:00
|
|
|
.space 1
|
|
|
|
|
|
|
|
.private_extern _private_external
|
|
|
|
_private_external:
|
|
|
|
.space 1
|
|
|
|
|
|
|
|
.weak_definition _private_external_weak
|
|
|
|
.private_extern _private_external_weak
|
|
|
|
_private_external_weak:
|
|
|
|
.space 1
|
[lld-macho][reland] Add basic symbol table output
This diff implements basic support for writing a symbol table.
Attributes are loosely supported for extern symbols and not at all for
other types.
Initial version by Kellie Medlin <kelliem@fb.com>
Originally committed in a3d95a50ee33 and reverted in fbae153ca583 due to
UBSAN erroring over unaligned writes. That has been fixed in the
current diff with the following changes:
```
diff --git a/lld/MachO/SyntheticSections.cpp b/lld/MachO/SyntheticSections.cpp
--- a/lld/MachO/SyntheticSections.cpp
+++ b/lld/MachO/SyntheticSections.cpp
@@ -133,6 +133,9 @@ SymtabSection::SymtabSection(StringTableSection &stringTableSection)
: stringTableSection(stringTableSection) {
segname = segment_names::linkEdit;
name = section_names::symbolTable;
+ // TODO: When we introduce the SyntheticSections superclass, we should make
+ // all synthetic sections aligned to WordSize by default.
+ align = WordSize;
}
size_t SymtabSection::getSize() const {
diff --git a/lld/MachO/Writer.cpp b/lld/MachO/Writer.cpp
--- a/lld/MachO/Writer.cpp
+++ b/lld/MachO/Writer.cpp
@@ -371,6 +371,7 @@ void Writer::assignAddresses(OutputSegment *seg) {
ArrayRef<InputSection *> sections = p.second;
for (InputSection *isec : sections) {
addr = alignTo(addr, isec->align);
+ // We must align the file offsets too to avoid misaligned writes of
+ // structs.
+ fileOff = alignTo(fileOff, isec->align);
isec->addr = addr;
addr += isec->getSize();
fileOff += isec->getFileSize();
@@ -396,6 +397,7 @@ void Writer::writeSections() {
uint64_t fileOff = seg->fileOff;
for (auto § : seg->getSections()) {
for (InputSection *isec : sect.second) {
+ fileOff = alignTo(fileOff, isec->align);
isec->writeTo(buf + fileOff);
fileOff += isec->getFileSize();
}
```
I don't think it's easy to write a test for alignment (that doesn't
involve brittly hard-coding file offsets), so there isn't one... but
UBSAN builds pass now.
Differential Revision: https://reviews.llvm.org/D79050
2020-04-29 07:58:19 +08:00
|
|
|
|
|
|
|
_main:
|
2021-05-18 01:49:17 +08:00
|
|
|
callq _private_external
|
2020-12-02 06:45:09 +08:00
|
|
|
callq _dynamic
|
[lld-macho][reland] Add basic symbol table output
This diff implements basic support for writing a symbol table.
Attributes are loosely supported for extern symbols and not at all for
other types.
Initial version by Kellie Medlin <kelliem@fb.com>
Originally committed in a3d95a50ee33 and reverted in fbae153ca583 due to
UBSAN erroring over unaligned writes. That has been fixed in the
current diff with the following changes:
```
diff --git a/lld/MachO/SyntheticSections.cpp b/lld/MachO/SyntheticSections.cpp
--- a/lld/MachO/SyntheticSections.cpp
+++ b/lld/MachO/SyntheticSections.cpp
@@ -133,6 +133,9 @@ SymtabSection::SymtabSection(StringTableSection &stringTableSection)
: stringTableSection(stringTableSection) {
segname = segment_names::linkEdit;
name = section_names::symbolTable;
+ // TODO: When we introduce the SyntheticSections superclass, we should make
+ // all synthetic sections aligned to WordSize by default.
+ align = WordSize;
}
size_t SymtabSection::getSize() const {
diff --git a/lld/MachO/Writer.cpp b/lld/MachO/Writer.cpp
--- a/lld/MachO/Writer.cpp
+++ b/lld/MachO/Writer.cpp
@@ -371,6 +371,7 @@ void Writer::assignAddresses(OutputSegment *seg) {
ArrayRef<InputSection *> sections = p.second;
for (InputSection *isec : sections) {
addr = alignTo(addr, isec->align);
+ // We must align the file offsets too to avoid misaligned writes of
+ // structs.
+ fileOff = alignTo(fileOff, isec->align);
isec->addr = addr;
addr += isec->getSize();
fileOff += isec->getFileSize();
@@ -396,6 +397,7 @@ void Writer::writeSections() {
uint64_t fileOff = seg->fileOff;
for (auto § : seg->getSections()) {
for (InputSection *isec : sect.second) {
+ fileOff = alignTo(fileOff, isec->align);
isec->writeTo(buf + fileOff);
fileOff += isec->getFileSize();
}
```
I don't think it's easy to write a test for alignment (that doesn't
involve brittly hard-coding file offsets), so there isn't one... but
UBSAN builds pass now.
Differential Revision: https://reviews.llvm.org/D79050
2020-04-29 07:58:19 +08:00
|
|
|
mov $0, %rax
|
|
|
|
ret
|
2021-05-18 01:49:17 +08:00
|
|
|
|
|
|
|
#--- test2.s
|
|
|
|
## These are both already in test.s and should make it into the symbol table
|
|
|
|
## just once.
|
|
|
|
.globl _external_weak, _private_external_weak
|
|
|
|
.text
|
|
|
|
.weak_definition _external_weak
|
|
|
|
_external_weak:
|
|
|
|
.space 1
|
|
|
|
.weak_definition _private_external_weak
|
|
|
|
.private_extern _private_external_weak
|
|
|
|
_private_external_weak:
|
|
|
|
.space 1
|