llvm-project/lld/MachO
Jez Ng 4f0cccdd7a [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 a3d95a50ee and reverted in fbae153ca5 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 &sect : 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-28 17:07:06 -07:00
..
Arch [lld-macho] Add basic support for linking against dylibs 2020-04-21 13:43:19 -07:00
CMakeLists.txt [lld-macho] Add basic support for linking against dylibs 2020-04-21 13:43:19 -07:00
Config.h Revert "[lld-macho] Add support for emitting dylibs with a single symbol" 2020-04-28 11:34:03 -07:00
Driver.cpp Revert "[lld-macho] Add support for emitting dylibs with a single symbol" 2020-04-28 11:34:03 -07:00
Driver.h Reland D75382 "[lld] Initial commit for new Mach-O backend" 2020-04-02 12:03:43 -07:00
InputFiles.cpp [lld-macho] Add basic support for linking against dylibs 2020-04-21 13:43:19 -07:00
InputFiles.h [lld-macho] Add basic support for linking against dylibs 2020-04-21 13:43:19 -07:00
InputSection.cpp [MachO] Fix UB in memcpy 2020-04-28 11:33:54 -07:00
InputSection.h [lld-macho] Extend SyntheticSections to cover all segment load commands 2020-04-27 12:58:12 -07:00
Options.td Revert "[lld-macho] Add support for emitting dylibs with a single symbol" 2020-04-28 11:34:03 -07:00
OutputSegment.cpp [lld-macho] Extend SyntheticSections to cover all segment load commands 2020-04-27 12:58:12 -07:00
OutputSegment.h [lld-macho] Extend SyntheticSections to cover all segment load commands 2020-04-27 12:58:12 -07:00
SymbolTable.cpp [lld-macho] Add basic support for linking against dylibs 2020-04-21 13:43:19 -07:00
SymbolTable.h [lld][macho] Fix implicit dependency on DenseMap.h include 2020-04-27 14:05:29 +01:00
Symbols.cpp Reland D75382 "[lld] Initial commit for new Mach-O backend" 2020-04-02 12:03:43 -07:00
Symbols.h [lld-macho] Add basic support for linking against dylibs 2020-04-21 13:43:19 -07:00
SyntheticSections.cpp [lld-macho][reland] Add basic symbol table output 2020-04-28 17:07:06 -07:00
SyntheticSections.h [lld-macho][reland] Add basic symbol table output 2020-04-28 17:07:06 -07:00
Target.cpp Reland D75382 "[lld] Initial commit for new Mach-O backend" 2020-04-02 12:03:43 -07:00
Target.h [lld-macho] Add basic support for linking against dylibs 2020-04-21 13:43:19 -07:00
Writer.cpp [lld-macho][reland] Add basic symbol table output 2020-04-28 17:07:06 -07:00
Writer.h [lld-macho] Extend SyntheticSections to cover all segment load commands 2020-04-27 12:58:12 -07:00