#include "Config.h"
#include "ExportTrie.h"
#include "InputSection.h"
+#include "MachOStructs.h"
#include "OutputSection.h"
#include "SymbolTable.h"
#include "Symbols.h"
}
}
-void InputFile::parseSymbols(ArrayRef<nlist_64> nList, const char *strtab,
- bool subsectionsViaSymbols) {
+void InputFile::parseSymbols(ArrayRef<structs::nlist_64> nList,
+ const char *strtab, bool subsectionsViaSymbols) {
// resize(), not reserve(), because we are going to create N_ALT_ENTRY symbols
// out-of-sequence.
symbols.resize(nList.size());
std::vector<size_t> altEntrySymIdxs;
- auto createDefined = [&](const nlist_64 &sym, InputSection *isec,
+ auto createDefined = [&](const structs::nlist_64 &sym, InputSection *isec,
uint32_t value) -> Symbol * {
StringRef name = strtab + sym.n_strx;
if (sym.n_type & N_EXT)
};
for (size_t i = 0, n = nList.size(); i < n; ++i) {
- const nlist_64 &sym = nList[i];
+ const structs::nlist_64 &sym = nList[i];
// Undefined symbol
if (!sym.n_sect) {
}
for (size_t idx : altEntrySymIdxs) {
- const nlist_64 &sym = nList[idx];
+ const structs::nlist_64 &sym = nList[idx];
SubsectionMap &subsecMap = subsections[sym.n_sect - 1];
uint32_t off = sym.n_value - sectionHeaders[sym.n_sect - 1].addr;
InputSection *subsec = findContainingSubsection(subsecMap, &off);
// TODO: Error on missing LC_SYMTAB?
if (const load_command *cmd = findCommand(hdr, LC_SYMTAB)) {
auto *c = reinterpret_cast<const symtab_command *>(cmd);
- ArrayRef<nlist_64> nList(
- reinterpret_cast<const nlist_64 *>(buf + c->symoff), c->nsyms);
+ ArrayRef<structs::nlist_64> nList(
+ reinterpret_cast<const structs::nlist_64 *>(buf + c->symoff), c->nsyms);
const char *strtab = reinterpret_cast<const char *>(buf) + c->stroff;
bool subsectionsViaSymbols = hdr->flags & MH_SUBSECTIONS_VIA_SYMBOLS;
parseSymbols(nList, strtab, subsectionsViaSymbols);
#ifndef LLD_MACHO_INPUT_FILES_H
#define LLD_MACHO_INPUT_FILES_H
+#include "MachOStructs.h"
+
#include "lld/Common/LLVM.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/BinaryFormat/MachO.h"
void parseSections(ArrayRef<llvm::MachO::section_64>);
- void parseSymbols(ArrayRef<llvm::MachO::nlist_64> nList, const char *strtab,
+ void parseSymbols(ArrayRef<lld::structs::nlist_64> nList, const char *strtab,
bool subsectionsViaSymbols);
void parseRelocations(const llvm::MachO::section_64 &, SubsectionMap &);
--- /dev/null
+//===- MachOStructs.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
+//
+//===----------------------------------------------------------------------===//
+//
+// This file defines structures used in the MachO object file format. Note that
+// unlike llvm/BinaryFormat/MachO.h, the structs here are defined in terms of
+// endian- and alignment-compatibility wrappers.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLD_MACHO_MACHO_STRUCTS_H
+#define LLD_MACHO_MACHO_STRUCTS_H
+
+#include "llvm/Support/Endian.h"
+
+namespace lld {
+
+namespace structs {
+
+struct nlist_64 {
+ llvm::support::ulittle32_t n_strx;
+ uint8_t n_type;
+ uint8_t n_sect;
+ llvm::support::ulittle16_t n_desc;
+ llvm::support::ulittle64_t n_value;
+};
+
+} // namespace structs
+
+} // namespace lld
+
+#endif
#include "Config.h"
#include "ExportTrie.h"
#include "InputFiles.h"
+#include "MachOStructs.h"
#include "OutputSegment.h"
#include "SymbolTable.h"
#include "Symbols.h"
}
size_t SymtabSection::getSize() const {
- return symbols.size() * sizeof(nlist_64);
+ return symbols.size() * sizeof(structs::nlist_64);
}
void SymtabSection::finalizeContents() {
}
void SymtabSection::writeTo(uint8_t *buf) const {
- auto *nList = reinterpret_cast<nlist_64 *>(buf);
+ auto *nList = reinterpret_cast<structs::nlist_64 *>(buf);
for (const SymtabEntry &entry : symbols) {
nList->n_strx = entry.strx;
// TODO support other symbol types
for (auto &p : seg->getSections()) {
OutputSection *section = p.second;
addr = alignTo(addr, section->align);
- // We must align the file offsets too to avoid misaligned writes of
- // structs.
fileOff = alignTo(fileOff, section->align);
section->addr = addr;
section->fileOff = fileOff;