2014-01-04 07:12:02 +08:00
|
|
|
//===- lib/ReaderWriter/MachO/MachONormalizedFileToAtoms.cpp --------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// 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
|
2014-01-04 07:12:02 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
///
|
|
|
|
/// \file Converts from in-memory normalized mach-o to in-memory Atoms.
|
|
|
|
///
|
|
|
|
/// +------------+
|
|
|
|
/// | normalized |
|
|
|
|
/// +------------+
|
|
|
|
/// |
|
|
|
|
/// |
|
|
|
|
/// v
|
|
|
|
/// +-------+
|
|
|
|
/// | Atoms |
|
|
|
|
/// +-------+
|
|
|
|
|
2014-07-17 03:49:02 +08:00
|
|
|
#include "ArchHandler.h"
|
2014-01-04 07:12:02 +08:00
|
|
|
#include "Atoms.h"
|
2014-07-17 03:49:02 +08:00
|
|
|
#include "File.h"
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "MachONormalizedFile.h"
|
2014-07-17 03:49:02 +08:00
|
|
|
#include "MachONormalizedFileBinaryUtils.h"
|
2017-10-03 05:00:41 +08:00
|
|
|
#include "lld/Common/LLVM.h"
|
2014-05-28 03:35:41 +08:00
|
|
|
#include "lld/Core/Error.h"
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/BinaryFormat/Dwarf.h"
|
|
|
|
#include "llvm/BinaryFormat/MachO.h"
|
2016-07-28 06:55:30 +08:00
|
|
|
#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
|
|
|
|
#include "llvm/Support/DataExtractor.h"
|
2015-12-17 06:03:21 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2016-07-28 06:55:30 +08:00
|
|
|
#include "llvm/Support/Error.h"
|
2014-06-28 02:25:01 +08:00
|
|
|
#include "llvm/Support/Format.h"
|
2015-05-13 08:44:47 +08:00
|
|
|
#include "llvm/Support/LEB128.h"
|
2015-12-17 06:03:21 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2014-01-04 07:12:02 +08:00
|
|
|
|
|
|
|
using namespace llvm::MachO;
|
2014-06-04 07:36:50 +08:00
|
|
|
using namespace lld::mach_o::normalized;
|
2014-01-04 07:12:02 +08:00
|
|
|
|
2015-12-17 06:03:21 +08:00
|
|
|
#define DEBUG_TYPE "normalized-file-to-atoms"
|
|
|
|
|
2014-01-04 07:12:02 +08:00
|
|
|
namespace lld {
|
|
|
|
namespace mach_o {
|
|
|
|
|
2014-05-30 07:07:20 +08:00
|
|
|
|
2014-06-04 07:36:50 +08:00
|
|
|
namespace { // anonymous
|
|
|
|
|
|
|
|
|
2014-06-10 09:50:00 +08:00
|
|
|
#define ENTRY(seg, sect, type, atomType) \
|
|
|
|
{seg, sect, type, DefinedAtom::atomType }
|
|
|
|
|
|
|
|
struct MachORelocatableSectionToAtomType {
|
|
|
|
StringRef segmentName;
|
|
|
|
StringRef sectionName;
|
|
|
|
SectionType sectionType;
|
|
|
|
DefinedAtom::ContentType atomType;
|
|
|
|
};
|
2014-06-04 07:36:50 +08:00
|
|
|
|
2014-06-10 09:50:00 +08:00
|
|
|
const MachORelocatableSectionToAtomType sectsToAtomType[] = {
|
|
|
|
ENTRY("__TEXT", "__text", S_REGULAR, typeCode),
|
2014-06-11 08:24:16 +08:00
|
|
|
ENTRY("__TEXT", "__text", S_REGULAR, typeResolver),
|
2014-06-10 09:50:00 +08:00
|
|
|
ENTRY("__TEXT", "__cstring", S_CSTRING_LITERALS, typeCString),
|
|
|
|
ENTRY("", "", S_CSTRING_LITERALS, typeCString),
|
|
|
|
ENTRY("__TEXT", "__ustring", S_REGULAR, typeUTF16String),
|
|
|
|
ENTRY("__TEXT", "__const", S_REGULAR, typeConstant),
|
2014-06-28 02:25:01 +08:00
|
|
|
ENTRY("__TEXT", "__const_coal", S_COALESCED, typeConstant),
|
2014-06-10 09:50:00 +08:00
|
|
|
ENTRY("__TEXT", "__eh_frame", S_COALESCED, typeCFI),
|
2014-07-22 06:06:57 +08:00
|
|
|
ENTRY("__TEXT", "__eh_frame", S_REGULAR, typeCFI),
|
2014-06-10 09:50:00 +08:00
|
|
|
ENTRY("__TEXT", "__literal4", S_4BYTE_LITERALS, typeLiteral4),
|
|
|
|
ENTRY("__TEXT", "__literal8", S_8BYTE_LITERALS, typeLiteral8),
|
|
|
|
ENTRY("__TEXT", "__literal16", S_16BYTE_LITERALS, typeLiteral16),
|
|
|
|
ENTRY("__TEXT", "__gcc_except_tab", S_REGULAR, typeLSDA),
|
|
|
|
ENTRY("__DATA", "__data", S_REGULAR, typeData),
|
2014-06-28 02:25:01 +08:00
|
|
|
ENTRY("__DATA", "__datacoal_nt", S_COALESCED, typeData),
|
2014-06-10 09:50:00 +08:00
|
|
|
ENTRY("__DATA", "__const", S_REGULAR, typeConstData),
|
|
|
|
ENTRY("__DATA", "__cfstring", S_REGULAR, typeCFString),
|
|
|
|
ENTRY("__DATA", "__mod_init_func", S_MOD_INIT_FUNC_POINTERS,
|
|
|
|
typeInitializerPtr),
|
|
|
|
ENTRY("__DATA", "__mod_term_func", S_MOD_TERM_FUNC_POINTERS,
|
|
|
|
typeTerminatorPtr),
|
2014-11-06 09:14:12 +08:00
|
|
|
ENTRY("__DATA", "__got", S_NON_LAZY_SYMBOL_POINTERS,
|
2014-06-10 09:50:00 +08:00
|
|
|
typeGOT),
|
2014-11-06 09:14:12 +08:00
|
|
|
ENTRY("__DATA", "__bss", S_ZEROFILL, typeZeroFill),
|
2014-06-10 09:50:00 +08:00
|
|
|
ENTRY("", "", S_NON_LAZY_SYMBOL_POINTERS,
|
|
|
|
typeGOT),
|
2014-11-06 09:09:13 +08:00
|
|
|
ENTRY("__DATA", "__interposing", S_INTERPOSING, typeInterposingTuples),
|
2015-06-24 04:35:31 +08:00
|
|
|
ENTRY("__DATA", "__thread_vars", S_THREAD_LOCAL_VARIABLES,
|
|
|
|
typeThunkTLV),
|
|
|
|
ENTRY("__DATA", "__thread_data", S_THREAD_LOCAL_REGULAR, typeTLVInitialData),
|
2015-12-12 07:25:09 +08:00
|
|
|
ENTRY("__DATA", "__thread_bss", S_THREAD_LOCAL_ZEROFILL,
|
|
|
|
typeTLVInitialZeroFill),
|
2016-01-20 05:54:21 +08:00
|
|
|
ENTRY("__DATA", "__objc_imageinfo", S_REGULAR, typeObjCImageInfo),
|
2016-02-02 03:10:10 +08:00
|
|
|
ENTRY("__DATA", "__objc_catlist", S_REGULAR, typeObjC2CategoryList),
|
2014-11-06 09:09:13 +08:00
|
|
|
ENTRY("", "", S_INTERPOSING, typeInterposingTuples),
|
2014-06-10 09:50:00 +08:00
|
|
|
ENTRY("__LD", "__compact_unwind", S_REGULAR,
|
2014-06-04 07:36:50 +08:00
|
|
|
typeCompactUnwindInfo),
|
2014-06-10 09:50:00 +08:00
|
|
|
ENTRY("", "", S_REGULAR, typeUnknown)
|
|
|
|
};
|
|
|
|
#undef ENTRY
|
2014-06-04 07:36:50 +08:00
|
|
|
|
2014-06-10 09:50:00 +08:00
|
|
|
|
|
|
|
/// Figures out ContentType of a mach-o section.
|
2014-10-03 01:27:20 +08:00
|
|
|
DefinedAtom::ContentType atomTypeFromSection(const Section §ion,
|
|
|
|
bool &customSectionName) {
|
2014-06-04 07:36:50 +08:00
|
|
|
// First look for match of name and type. Empty names in table are wildcards.
|
2014-10-03 01:27:20 +08:00
|
|
|
customSectionName = false;
|
2014-06-04 07:36:50 +08:00
|
|
|
for (const MachORelocatableSectionToAtomType *p = sectsToAtomType ;
|
|
|
|
p->atomType != DefinedAtom::typeUnknown; ++p) {
|
|
|
|
if (p->sectionType != section.type)
|
2014-01-08 10:52:58 +08:00
|
|
|
continue;
|
2014-06-04 07:36:50 +08:00
|
|
|
if (!p->segmentName.equals(section.segmentName) && !p->segmentName.empty())
|
2014-01-14 06:28:02 +08:00
|
|
|
continue;
|
2014-06-04 07:36:50 +08:00
|
|
|
if (!p->sectionName.equals(section.sectionName) && !p->sectionName.empty())
|
2014-01-14 06:28:02 +08:00
|
|
|
continue;
|
2014-10-03 01:27:20 +08:00
|
|
|
customSectionName = p->segmentName.empty() && p->sectionName.empty();
|
2014-06-04 07:36:50 +08:00
|
|
|
return p->atomType;
|
2014-01-14 06:28:02 +08:00
|
|
|
}
|
2014-06-04 07:36:50 +08:00
|
|
|
// Look for code denoted by section attributes
|
|
|
|
if (section.attributes & S_ATTR_PURE_INSTRUCTIONS)
|
|
|
|
return DefinedAtom::typeCode;
|
|
|
|
|
|
|
|
return DefinedAtom::typeUnknown;
|
2014-01-08 10:52:58 +08:00
|
|
|
}
|
|
|
|
|
2014-06-04 07:36:50 +08:00
|
|
|
enum AtomizeModel {
|
|
|
|
atomizeAtSymbols,
|
|
|
|
atomizeFixedSize,
|
|
|
|
atomizePointerSize,
|
|
|
|
atomizeUTF8,
|
|
|
|
atomizeUTF16,
|
|
|
|
atomizeCFI,
|
2014-06-28 02:25:01 +08:00
|
|
|
atomizeCU,
|
|
|
|
atomizeCFString
|
2014-06-04 07:36:50 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Returns info on how to atomize a section of the specified ContentType.
|
|
|
|
void sectionParseInfo(DefinedAtom::ContentType atomType,
|
|
|
|
unsigned int &sizeMultiple,
|
|
|
|
DefinedAtom::Scope &scope,
|
|
|
|
DefinedAtom::Merge &merge,
|
|
|
|
AtomizeModel &atomizeModel) {
|
|
|
|
struct ParseInfo {
|
|
|
|
DefinedAtom::ContentType atomType;
|
|
|
|
unsigned int sizeMultiple;
|
|
|
|
DefinedAtom::Scope scope;
|
|
|
|
DefinedAtom::Merge merge;
|
|
|
|
AtomizeModel atomizeModel;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define ENTRY(type, size, scope, merge, model) \
|
|
|
|
{DefinedAtom::type, size, DefinedAtom::scope, DefinedAtom::merge, model }
|
|
|
|
|
|
|
|
static const ParseInfo parseInfo[] = {
|
2015-02-23 07:54:38 +08:00
|
|
|
ENTRY(typeCode, 1, scopeGlobal, mergeNo,
|
2014-06-04 07:36:50 +08:00
|
|
|
atomizeAtSymbols),
|
2015-02-23 07:54:38 +08:00
|
|
|
ENTRY(typeData, 1, scopeGlobal, mergeNo,
|
2014-06-04 07:36:50 +08:00
|
|
|
atomizeAtSymbols),
|
2015-02-23 07:54:38 +08:00
|
|
|
ENTRY(typeConstData, 1, scopeGlobal, mergeNo,
|
2014-06-04 07:36:50 +08:00
|
|
|
atomizeAtSymbols),
|
2015-02-23 07:54:38 +08:00
|
|
|
ENTRY(typeZeroFill, 1, scopeGlobal, mergeNo,
|
2014-06-04 07:36:50 +08:00
|
|
|
atomizeAtSymbols),
|
2015-02-23 07:54:38 +08:00
|
|
|
ENTRY(typeConstant, 1, scopeGlobal, mergeNo,
|
2014-06-04 07:36:50 +08:00
|
|
|
atomizeAtSymbols),
|
2015-02-23 07:54:38 +08:00
|
|
|
ENTRY(typeCString, 1, scopeLinkageUnit, mergeByContent,
|
2014-06-04 07:36:50 +08:00
|
|
|
atomizeUTF8),
|
2015-02-23 07:54:38 +08:00
|
|
|
ENTRY(typeUTF16String, 1, scopeLinkageUnit, mergeByContent,
|
2014-06-04 07:36:50 +08:00
|
|
|
atomizeUTF16),
|
2014-07-22 06:06:57 +08:00
|
|
|
ENTRY(typeCFI, 4, scopeTranslationUnit, mergeNo,
|
2014-06-04 07:36:50 +08:00
|
|
|
atomizeCFI),
|
2015-02-23 07:54:38 +08:00
|
|
|
ENTRY(typeLiteral4, 4, scopeLinkageUnit, mergeByContent,
|
2014-06-04 07:36:50 +08:00
|
|
|
atomizeFixedSize),
|
2015-02-23 07:54:38 +08:00
|
|
|
ENTRY(typeLiteral8, 8, scopeLinkageUnit, mergeByContent,
|
2014-06-04 07:36:50 +08:00
|
|
|
atomizeFixedSize),
|
2015-02-23 07:54:38 +08:00
|
|
|
ENTRY(typeLiteral16, 16, scopeLinkageUnit, mergeByContent,
|
2014-06-04 07:36:50 +08:00
|
|
|
atomizeFixedSize),
|
2015-02-23 07:54:38 +08:00
|
|
|
ENTRY(typeCFString, 4, scopeLinkageUnit, mergeByContent,
|
2014-06-28 02:25:01 +08:00
|
|
|
atomizeCFString),
|
2015-02-23 07:54:38 +08:00
|
|
|
ENTRY(typeInitializerPtr, 4, scopeTranslationUnit, mergeNo,
|
2014-06-04 07:36:50 +08:00
|
|
|
atomizePointerSize),
|
2015-02-23 07:54:38 +08:00
|
|
|
ENTRY(typeTerminatorPtr, 4, scopeTranslationUnit, mergeNo,
|
2014-06-04 07:36:50 +08:00
|
|
|
atomizePointerSize),
|
2015-02-23 07:54:38 +08:00
|
|
|
ENTRY(typeCompactUnwindInfo, 4, scopeTranslationUnit, mergeNo,
|
2014-06-04 07:36:50 +08:00
|
|
|
atomizeCU),
|
2015-02-23 07:54:38 +08:00
|
|
|
ENTRY(typeGOT, 4, scopeLinkageUnit, mergeByContent,
|
2014-06-04 07:36:50 +08:00
|
|
|
atomizePointerSize),
|
2016-02-02 03:10:10 +08:00
|
|
|
ENTRY(typeObjC2CategoryList, 4, scopeTranslationUnit, mergeByContent,
|
|
|
|
atomizePointerSize),
|
2015-02-23 07:54:38 +08:00
|
|
|
ENTRY(typeUnknown, 1, scopeGlobal, mergeNo,
|
2014-06-04 07:36:50 +08:00
|
|
|
atomizeAtSymbols)
|
|
|
|
};
|
|
|
|
#undef ENTRY
|
|
|
|
const int tableLen = sizeof(parseInfo) / sizeof(ParseInfo);
|
|
|
|
for (int i=0; i < tableLen; ++i) {
|
|
|
|
if (parseInfo[i].atomType == atomType) {
|
|
|
|
sizeMultiple = parseInfo[i].sizeMultiple;
|
|
|
|
scope = parseInfo[i].scope;
|
|
|
|
merge = parseInfo[i].merge;
|
|
|
|
atomizeModel = parseInfo[i].atomizeModel;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unknown type is atomized by symbols.
|
|
|
|
sizeMultiple = 1;
|
|
|
|
scope = DefinedAtom::scopeGlobal;
|
|
|
|
merge = DefinedAtom::mergeNo;
|
|
|
|
atomizeModel = atomizeAtSymbols;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Atom::Scope atomScope(uint8_t scope) {
|
2014-01-14 06:28:02 +08:00
|
|
|
switch (scope) {
|
|
|
|
case N_EXT:
|
|
|
|
return Atom::scopeGlobal;
|
2014-06-30 17:11:38 +08:00
|
|
|
case N_PEXT:
|
2014-01-14 06:28:02 +08:00
|
|
|
case N_PEXT | N_EXT:
|
|
|
|
return Atom::scopeLinkageUnit;
|
|
|
|
case 0:
|
|
|
|
return Atom::scopeTranslationUnit;
|
|
|
|
}
|
|
|
|
llvm_unreachable("unknown scope value!");
|
|
|
|
}
|
|
|
|
|
2020-07-02 12:02:09 +08:00
|
|
|
void appendSymbolsInSection(
|
|
|
|
const std::vector<lld::mach_o::normalized::Symbol> &inSymbols,
|
|
|
|
uint32_t sectionIndex,
|
|
|
|
SmallVector<const lld::mach_o::normalized::Symbol *, 64> &outSyms) {
|
|
|
|
for (const lld::mach_o::normalized::Symbol &sym : inSymbols) {
|
2014-06-04 07:36:50 +08:00
|
|
|
// Only look at definition symbols.
|
|
|
|
if ((sym.type & N_TYPE) != N_SECT)
|
|
|
|
continue;
|
|
|
|
if (sym.sect != sectionIndex)
|
|
|
|
continue;
|
|
|
|
outSyms.push_back(&sym);
|
2014-05-31 06:51:04 +08:00
|
|
|
}
|
2014-05-16 07:03:50 +08:00
|
|
|
}
|
|
|
|
|
2014-06-04 07:36:50 +08:00
|
|
|
void atomFromSymbol(DefinedAtom::ContentType atomType, const Section §ion,
|
2014-06-04 08:34:27 +08:00
|
|
|
MachOFile &file, uint64_t symbolAddr, StringRef symbolName,
|
2014-06-11 08:24:16 +08:00
|
|
|
uint16_t symbolDescFlags, Atom::Scope symbolScope,
|
2014-10-22 07:45:37 +08:00
|
|
|
uint64_t nextSymbolAddr, bool scatterable, bool copyRefs) {
|
2014-06-04 07:36:50 +08:00
|
|
|
// Mach-O symbol table does have size in it. Instead the size is the
|
|
|
|
// difference between this and the next symbol.
|
2014-06-04 08:34:27 +08:00
|
|
|
uint64_t size = nextSymbolAddr - symbolAddr;
|
2014-06-28 02:25:01 +08:00
|
|
|
uint64_t offset = symbolAddr - section.address;
|
2014-10-22 07:45:37 +08:00
|
|
|
bool noDeadStrip = (symbolDescFlags & N_NO_DEAD_STRIP) || !scatterable;
|
2015-12-12 07:25:09 +08:00
|
|
|
if (isZeroFillSection(section.type)) {
|
2014-08-22 06:18:30 +08:00
|
|
|
file.addZeroFillDefinedAtom(symbolName, symbolScope, offset, size,
|
|
|
|
noDeadStrip, copyRefs, §ion);
|
2014-05-16 07:03:50 +08:00
|
|
|
} else {
|
2014-06-28 02:25:01 +08:00
|
|
|
DefinedAtom::Merge merge = (symbolDescFlags & N_WEAK_DEF)
|
2014-06-04 08:34:27 +08:00
|
|
|
? DefinedAtom::mergeAsWeak : DefinedAtom::mergeNo;
|
2014-07-04 08:11:09 +08:00
|
|
|
bool thumb = (symbolDescFlags & N_ARM_THUMB_DEF);
|
2014-06-04 07:36:50 +08:00
|
|
|
if (atomType == DefinedAtom::typeUnknown) {
|
2020-01-07 02:21:05 +08:00
|
|
|
// Mach-O needs a segment and section name. Concatenate those two
|
2014-09-04 03:52:50 +08:00
|
|
|
// with a / separator (e.g. "seg/sect") to fit into the lld model
|
2014-05-31 06:51:04 +08:00
|
|
|
// of just a section name.
|
2014-06-04 07:36:50 +08:00
|
|
|
std::string segSectName = section.segmentName.str()
|
2014-05-31 06:51:04 +08:00
|
|
|
+ "/" + section.sectionName.str();
|
2014-06-04 08:34:27 +08:00
|
|
|
file.addDefinedAtomInCustomSection(symbolName, symbolScope, atomType,
|
2014-08-22 06:18:30 +08:00
|
|
|
merge, thumb, noDeadStrip, offset,
|
|
|
|
size, segSectName, true, §ion);
|
2014-05-31 06:51:04 +08:00
|
|
|
} else {
|
2014-06-28 02:25:01 +08:00
|
|
|
if ((atomType == lld::DefinedAtom::typeCode) &&
|
2014-06-11 08:24:16 +08:00
|
|
|
(symbolDescFlags & N_SYMBOL_RESOLVER)) {
|
2014-06-28 02:25:01 +08:00
|
|
|
atomType = lld::DefinedAtom::typeResolver;
|
2014-06-11 08:24:16 +08:00
|
|
|
}
|
2014-06-04 08:34:27 +08:00
|
|
|
file.addDefinedAtom(symbolName, symbolScope, atomType, merge,
|
2014-08-22 06:18:30 +08:00
|
|
|
offset, size, thumb, noDeadStrip, copyRefs, §ion);
|
2014-05-31 06:51:04 +08:00
|
|
|
}
|
2014-05-16 07:03:50 +08:00
|
|
|
}
|
2014-01-13 06:55:49 +08:00
|
|
|
}
|
|
|
|
|
2016-03-31 07:43:27 +08:00
|
|
|
llvm::Error processSymboledSection(DefinedAtom::ContentType atomType,
|
|
|
|
const Section §ion,
|
|
|
|
const NormalizedFile &normalizedFile,
|
|
|
|
MachOFile &file, bool scatterable,
|
|
|
|
bool copyRefs) {
|
2014-06-04 07:36:50 +08:00
|
|
|
// Find section's index.
|
|
|
|
uint32_t sectIndex = 1;
|
|
|
|
for (auto § : normalizedFile.sections) {
|
|
|
|
if (§ == §ion)
|
|
|
|
break;
|
|
|
|
++sectIndex;
|
2014-05-16 04:59:23 +08:00
|
|
|
}
|
|
|
|
|
2014-06-04 07:36:50 +08:00
|
|
|
// Find all symbols in this section.
|
2020-07-02 12:02:09 +08:00
|
|
|
SmallVector<const lld::mach_o::normalized::Symbol *, 64> symbols;
|
2014-06-04 07:36:50 +08:00
|
|
|
appendSymbolsInSection(normalizedFile.globalSymbols, sectIndex, symbols);
|
|
|
|
appendSymbolsInSection(normalizedFile.localSymbols, sectIndex, symbols);
|
2014-05-30 04:44:21 +08:00
|
|
|
|
2014-06-10 07:35:37 +08:00
|
|
|
// Sort symbols.
|
2014-06-04 07:36:50 +08:00
|
|
|
std::sort(symbols.begin(), symbols.end(),
|
2020-07-02 12:02:09 +08:00
|
|
|
[](const lld::mach_o::normalized::Symbol *lhs,
|
|
|
|
const lld::mach_o::normalized::Symbol *rhs) -> bool {
|
2014-06-28 02:25:01 +08:00
|
|
|
if (lhs == rhs)
|
|
|
|
return false;
|
2014-06-10 07:35:37 +08:00
|
|
|
// First by address.
|
2014-06-28 02:25:01 +08:00
|
|
|
uint64_t lhsAddr = lhs->value;
|
|
|
|
uint64_t rhsAddr = rhs->value;
|
|
|
|
if (lhsAddr != rhsAddr)
|
|
|
|
return lhsAddr < rhsAddr;
|
2020-07-02 12:02:09 +08:00
|
|
|
// If same address, one is an alias so sort by scope.
|
2014-06-10 07:35:37 +08:00
|
|
|
Atom::Scope lScope = atomScope(lhs->scope);
|
|
|
|
Atom::Scope rScope = atomScope(rhs->scope);
|
|
|
|
if (lScope != rScope)
|
|
|
|
return lScope < rScope;
|
2014-09-10 07:52:59 +08:00
|
|
|
// If same address and scope, see if one might be better as
|
|
|
|
// the alias.
|
|
|
|
bool lPrivate = (lhs->name.front() == 'l');
|
|
|
|
bool rPrivate = (rhs->name.front() == 'l');
|
|
|
|
if (lPrivate != rPrivate)
|
|
|
|
return lPrivate;
|
2014-06-25 23:12:55 +08:00
|
|
|
// If same address and scope, sort by name.
|
|
|
|
return lhs->name < rhs->name;
|
2014-06-04 07:36:50 +08:00
|
|
|
});
|
2014-05-30 04:44:21 +08:00
|
|
|
|
2014-06-04 07:36:50 +08:00
|
|
|
// Debug logging of symbols.
|
Make it possible to redirect not only errs() but also outs()
This change is for those who use lld as a library. Context:
https://reviews.llvm.org/D70287
This patch adds a new parmeter to lld::*::link() so that we can pass
an raw_ostream object representing stdout. Previously, lld::*::link()
took only an stderr object.
Justification for making stdoutOS and stderrOS mandatory: I wanted to
make link() functions to take stdout and stderr in that order.
However, if we change the function signature from
bool link(ArrayRef<const char *> args, bool canExitEarly,
raw_ostream &stderrOS = llvm::errs());
to
bool link(ArrayRef<const char *> args, bool canExitEarly,
raw_ostream &stdoutOS = llvm::outs(),
raw_ostream &stderrOS = llvm::errs());
, then the meaning of existing code that passes stderrOS silently
changes (stderrOS would be interpreted as stdoutOS). So, I chose to
make existing code not to compile, so that developers can fix their
code.
Differential Revision: https://reviews.llvm.org/D70292
2019-11-15 13:06:57 +08:00
|
|
|
// for (const Symbol *sym : symbols)
|
2019-11-19 10:16:59 +08:00
|
|
|
// llvm::errs() << " sym: "
|
2015-02-23 07:54:38 +08:00
|
|
|
// << llvm::format("0x%08llx ", (uint64_t)sym->value)
|
2014-06-28 02:25:01 +08:00
|
|
|
// << ", " << sym->name << "\n";
|
2014-05-30 07:07:20 +08:00
|
|
|
|
2014-06-04 07:36:50 +08:00
|
|
|
// If section has no symbols and no content, there are no atoms.
|
|
|
|
if (symbols.empty() && section.content.empty())
|
2016-11-11 12:29:25 +08:00
|
|
|
return llvm::Error::success();
|
2014-06-04 07:36:50 +08:00
|
|
|
|
2014-06-28 02:25:01 +08:00
|
|
|
if (symbols.empty()) {
|
2020-01-07 02:21:05 +08:00
|
|
|
// Section has no symbols, put all content in one anonymous atom.
|
2014-06-28 02:25:01 +08:00
|
|
|
atomFromSymbol(atomType, section, file, section.address, StringRef(),
|
|
|
|
0, Atom::scopeTranslationUnit,
|
2014-10-22 07:45:37 +08:00
|
|
|
section.address + section.content.size(),
|
|
|
|
scatterable, copyRefs);
|
2014-06-28 02:25:01 +08:00
|
|
|
}
|
|
|
|
else if (symbols.front()->value != section.address) {
|
2014-06-04 07:36:50 +08:00
|
|
|
// Section has anonymous content before first symbol.
|
2014-06-28 02:25:01 +08:00
|
|
|
atomFromSymbol(atomType, section, file, section.address, StringRef(),
|
|
|
|
0, Atom::scopeTranslationUnit, symbols.front()->value,
|
2014-10-22 07:45:37 +08:00
|
|
|
scatterable, copyRefs);
|
2014-05-30 07:07:20 +08:00
|
|
|
}
|
|
|
|
|
2020-07-02 12:02:09 +08:00
|
|
|
const lld::mach_o::normalized::Symbol *lastSym = nullptr;
|
|
|
|
for (const lld::mach_o::normalized::Symbol *sym : symbols) {
|
2014-06-04 07:36:50 +08:00
|
|
|
if (lastSym != nullptr) {
|
2014-09-10 07:52:59 +08:00
|
|
|
// Ignore any assembler added "ltmpNNN" symbol at start of section
|
|
|
|
// if there is another symbol at the start.
|
|
|
|
if ((lastSym->value != sym->value)
|
|
|
|
|| lastSym->value != section.address
|
|
|
|
|| !lastSym->name.startswith("ltmp")) {
|
|
|
|
atomFromSymbol(atomType, section, file, lastSym->value, lastSym->name,
|
|
|
|
lastSym->desc, atomScope(lastSym->scope), sym->value,
|
2014-10-22 07:45:37 +08:00
|
|
|
scatterable, copyRefs);
|
2014-09-10 07:52:59 +08:00
|
|
|
}
|
2014-06-04 07:36:50 +08:00
|
|
|
}
|
|
|
|
lastSym = sym;
|
2014-05-30 07:50:48 +08:00
|
|
|
}
|
2014-06-04 07:36:50 +08:00
|
|
|
if (lastSym != nullptr) {
|
2014-06-04 08:34:27 +08:00
|
|
|
atomFromSymbol(atomType, section, file, lastSym->value, lastSym->name,
|
2014-06-11 08:24:16 +08:00
|
|
|
lastSym->desc, atomScope(lastSym->scope),
|
2014-10-22 07:45:37 +08:00
|
|
|
section.address + section.content.size(),
|
|
|
|
scatterable, copyRefs);
|
2014-05-30 07:50:48 +08:00
|
|
|
}
|
2014-10-22 07:45:37 +08:00
|
|
|
|
|
|
|
// If object built without .subsections_via_symbols, add reference chain.
|
|
|
|
if (!scatterable) {
|
|
|
|
MachODefinedAtom *prevAtom = nullptr;
|
|
|
|
file.eachAtomInSection(section,
|
|
|
|
[&](MachODefinedAtom *atom, uint64_t offset)->void {
|
|
|
|
if (prevAtom)
|
2016-02-02 07:42:10 +08:00
|
|
|
prevAtom->addReference(Reference::KindNamespace::all,
|
2014-10-22 07:45:37 +08:00
|
|
|
Reference::KindArch::all,
|
2016-02-02 07:42:10 +08:00
|
|
|
Reference::kindLayoutAfter, 0, atom, 0);
|
2014-10-22 07:45:37 +08:00
|
|
|
prevAtom = atom;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-11-11 12:29:25 +08:00
|
|
|
return llvm::Error::success();
|
2014-05-30 07:50:48 +08:00
|
|
|
}
|
|
|
|
|
2016-03-31 07:43:27 +08:00
|
|
|
llvm::Error processSection(DefinedAtom::ContentType atomType,
|
|
|
|
const Section §ion,
|
|
|
|
bool customSectionName,
|
|
|
|
const NormalizedFile &normalizedFile,
|
|
|
|
MachOFile &file, bool scatterable,
|
|
|
|
bool copyRefs) {
|
2014-06-04 07:36:50 +08:00
|
|
|
const bool is64 = MachOLinkingContext::is64Bit(normalizedFile.arch);
|
2014-10-28 06:48:35 +08:00
|
|
|
const bool isBig = MachOLinkingContext::isBigEndian(normalizedFile.arch);
|
2014-06-04 07:36:50 +08:00
|
|
|
|
|
|
|
// Get info on how to atomize section.
|
|
|
|
unsigned int sizeMultiple;
|
|
|
|
DefinedAtom::Scope scope;
|
|
|
|
DefinedAtom::Merge merge;
|
|
|
|
AtomizeModel atomizeModel;
|
|
|
|
sectionParseInfo(atomType, sizeMultiple, scope, merge, atomizeModel);
|
|
|
|
|
|
|
|
// Validate section size.
|
|
|
|
if ((section.content.size() % sizeMultiple) != 0)
|
2016-03-31 07:43:27 +08:00
|
|
|
return llvm::make_error<GenericError>(Twine("Section ")
|
|
|
|
+ section.segmentName
|
|
|
|
+ "/" + section.sectionName
|
|
|
|
+ " has size ("
|
|
|
|
+ Twine(section.content.size())
|
|
|
|
+ ") which is not a multiple of "
|
|
|
|
+ Twine(sizeMultiple));
|
2014-06-04 07:36:50 +08:00
|
|
|
|
|
|
|
if (atomizeModel == atomizeAtSymbols) {
|
|
|
|
// Break section up into atoms each with a fixed size.
|
|
|
|
return processSymboledSection(atomType, section, normalizedFile, file,
|
2014-10-22 07:45:37 +08:00
|
|
|
scatterable, copyRefs);
|
2014-06-04 07:36:50 +08:00
|
|
|
} else {
|
|
|
|
unsigned int size;
|
|
|
|
for (unsigned int offset = 0, e = section.content.size(); offset != e;) {
|
|
|
|
switch (atomizeModel) {
|
|
|
|
case atomizeFixedSize:
|
|
|
|
// Break section up into atoms each with a fixed size.
|
|
|
|
size = sizeMultiple;
|
|
|
|
break;
|
|
|
|
case atomizePointerSize:
|
|
|
|
// Break section up into atoms each the size of a pointer.
|
2014-06-28 02:25:01 +08:00
|
|
|
size = is64 ? 8 : 4;
|
2014-06-04 07:36:50 +08:00
|
|
|
break;
|
|
|
|
case atomizeUTF8:
|
|
|
|
// Break section up into zero terminated c-strings.
|
|
|
|
size = 0;
|
2014-06-25 19:21:51 +08:00
|
|
|
for (unsigned int i = offset; i < e; ++i) {
|
2014-06-04 07:36:50 +08:00
|
|
|
if (section.content[i] == 0) {
|
2014-06-25 19:21:51 +08:00
|
|
|
size = i + 1 - offset;
|
2014-06-04 07:36:50 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case atomizeUTF16:
|
|
|
|
// Break section up into zero terminated UTF16 strings.
|
|
|
|
size = 0;
|
2014-06-25 19:21:51 +08:00
|
|
|
for (unsigned int i = offset; i < e; i += 2) {
|
|
|
|
if ((section.content[i] == 0) && (section.content[i + 1] == 0)) {
|
|
|
|
size = i + 2 - offset;
|
2014-06-04 07:36:50 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case atomizeCFI:
|
|
|
|
// Break section up into dwarf unwind CFIs (FDE or CIE).
|
2014-10-28 06:48:35 +08:00
|
|
|
size = read32(§ion.content[offset], isBig) + 4;
|
2014-06-04 07:36:50 +08:00
|
|
|
if (offset+size > section.content.size()) {
|
2016-03-31 07:43:27 +08:00
|
|
|
return llvm::make_error<GenericError>(Twine("Section ")
|
|
|
|
+ section.segmentName
|
|
|
|
+ "/" + section.sectionName
|
|
|
|
+ " is malformed. Size of CFI "
|
|
|
|
"starting at offset ("
|
|
|
|
+ Twine(offset)
|
|
|
|
+ ") is past end of section.");
|
2014-06-04 07:36:50 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case atomizeCU:
|
|
|
|
// Break section up into compact unwind entries.
|
|
|
|
size = is64 ? 32 : 20;
|
|
|
|
break;
|
2014-06-28 02:25:01 +08:00
|
|
|
case atomizeCFString:
|
2014-07-01 04:44:47 +08:00
|
|
|
// Break section up into NS/CFString objects.
|
2014-06-28 02:25:01 +08:00
|
|
|
size = is64 ? 32 : 16;
|
|
|
|
break;
|
2014-06-04 07:36:50 +08:00
|
|
|
case atomizeAtSymbols:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (size == 0) {
|
2016-03-31 07:43:27 +08:00
|
|
|
return llvm::make_error<GenericError>(Twine("Section ")
|
|
|
|
+ section.segmentName
|
|
|
|
+ "/" + section.sectionName
|
|
|
|
+ " is malformed. The last atom "
|
|
|
|
"is not zero terminated.");
|
2014-05-22 09:42:06 +08:00
|
|
|
}
|
2014-10-03 01:27:20 +08:00
|
|
|
if (customSectionName) {
|
2020-01-07 02:21:05 +08:00
|
|
|
// Mach-O needs a segment and section name. Concatenate those two
|
2014-10-03 01:27:20 +08:00
|
|
|
// with a / separator (e.g. "seg/sect") to fit into the lld model
|
|
|
|
// of just a section name.
|
|
|
|
std::string segSectName = section.segmentName.str()
|
|
|
|
+ "/" + section.sectionName.str();
|
|
|
|
file.addDefinedAtomInCustomSection(StringRef(), scope, atomType,
|
|
|
|
merge, false, false, offset,
|
|
|
|
size, segSectName, true, §ion);
|
|
|
|
} else {
|
|
|
|
file.addDefinedAtom(StringRef(), scope, atomType, merge, offset, size,
|
|
|
|
false, false, copyRefs, §ion);
|
|
|
|
}
|
2014-06-04 07:36:50 +08:00
|
|
|
offset += size;
|
2014-05-22 09:42:06 +08:00
|
|
|
}
|
|
|
|
}
|
2016-11-11 12:29:25 +08:00
|
|
|
return llvm::Error::success();
|
2014-05-22 09:42:06 +08:00
|
|
|
}
|
2014-05-16 04:59:23 +08:00
|
|
|
|
2014-07-25 07:06:56 +08:00
|
|
|
const Section* findSectionCoveringAddress(const NormalizedFile &normalizedFile,
|
|
|
|
uint64_t address) {
|
|
|
|
for (const Section &s : normalizedFile.sections) {
|
|
|
|
uint64_t sAddr = s.address;
|
|
|
|
if ((sAddr <= address) && (address < sAddr+s.content.size())) {
|
|
|
|
return &s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-10-16 02:19:31 +08:00
|
|
|
const MachODefinedAtom *
|
|
|
|
findAtomCoveringAddress(const NormalizedFile &normalizedFile, MachOFile &file,
|
2016-07-28 06:55:30 +08:00
|
|
|
uint64_t addr, Reference::Addend &addend) {
|
2014-10-16 02:19:31 +08:00
|
|
|
const Section *sect = nullptr;
|
|
|
|
sect = findSectionCoveringAddress(normalizedFile, addr);
|
|
|
|
if (!sect)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
uint32_t offsetInTarget;
|
|
|
|
uint64_t offsetInSect = addr - sect->address;
|
|
|
|
auto atom =
|
|
|
|
file.findAtomCoveringAddress(*sect, offsetInSect, &offsetInTarget);
|
2016-07-28 06:55:30 +08:00
|
|
|
addend = offsetInTarget;
|
2014-10-16 02:19:31 +08:00
|
|
|
return atom;
|
|
|
|
}
|
|
|
|
|
2014-06-28 02:25:01 +08:00
|
|
|
// Walks all relocations for a section in a normalized .o file and
|
|
|
|
// creates corresponding lld::Reference objects.
|
2016-03-31 04:15:06 +08:00
|
|
|
llvm::Error convertRelocs(const Section §ion,
|
|
|
|
const NormalizedFile &normalizedFile,
|
|
|
|
bool scatterable,
|
|
|
|
MachOFile &file,
|
|
|
|
ArchHandler &handler) {
|
2014-07-17 03:49:02 +08:00
|
|
|
// Utility function for ArchHandler to find atom by its address.
|
2014-06-28 02:25:01 +08:00
|
|
|
auto atomByAddr = [&] (uint32_t sectIndex, uint64_t addr,
|
|
|
|
const lld::Atom **atom, Reference::Addend *addend)
|
2016-03-31 04:15:06 +08:00
|
|
|
-> llvm::Error {
|
2014-06-28 02:25:01 +08:00
|
|
|
if (sectIndex > normalizedFile.sections.size())
|
2016-03-31 04:15:06 +08:00
|
|
|
return llvm::make_error<GenericError>(Twine("out of range section "
|
2014-06-28 02:25:01 +08:00
|
|
|
"index (") + Twine(sectIndex) + ")");
|
2014-07-03 07:52:22 +08:00
|
|
|
const Section *sect = nullptr;
|
|
|
|
if (sectIndex == 0) {
|
2014-07-25 07:06:56 +08:00
|
|
|
sect = findSectionCoveringAddress(normalizedFile, addr);
|
|
|
|
if (!sect)
|
2016-03-31 04:15:06 +08:00
|
|
|
return llvm::make_error<GenericError>(Twine("address (" + Twine(addr)
|
2014-07-25 07:06:56 +08:00
|
|
|
+ ") is not in any section"));
|
2014-07-03 07:52:22 +08:00
|
|
|
} else {
|
|
|
|
sect = &normalizedFile.sections[sectIndex-1];
|
|
|
|
}
|
2014-06-28 02:25:01 +08:00
|
|
|
uint32_t offsetInTarget;
|
2014-07-03 07:52:22 +08:00
|
|
|
uint64_t offsetInSect = addr - sect->address;
|
|
|
|
*atom = file.findAtomCoveringAddress(*sect, offsetInSect, &offsetInTarget);
|
2014-06-28 02:25:01 +08:00
|
|
|
*addend = offsetInTarget;
|
2016-11-11 12:29:25 +08:00
|
|
|
return llvm::Error::success();
|
2014-06-28 02:25:01 +08:00
|
|
|
};
|
|
|
|
|
2014-07-17 03:49:02 +08:00
|
|
|
// Utility function for ArchHandler to find atom by its symbol index.
|
2014-06-28 02:25:01 +08:00
|
|
|
auto atomBySymbol = [&] (uint32_t symbolIndex, const lld::Atom **result)
|
2016-03-31 04:15:06 +08:00
|
|
|
-> llvm::Error {
|
2014-06-28 02:25:01 +08:00
|
|
|
// Find symbol from index.
|
2020-07-02 12:02:09 +08:00
|
|
|
const lld::mach_o::normalized::Symbol *sym = nullptr;
|
2016-07-28 06:55:30 +08:00
|
|
|
uint32_t numStabs = normalizedFile.stabsSymbols.size();
|
2014-06-28 02:25:01 +08:00
|
|
|
uint32_t numLocal = normalizedFile.localSymbols.size();
|
|
|
|
uint32_t numGlobal = normalizedFile.globalSymbols.size();
|
|
|
|
uint32_t numUndef = normalizedFile.undefinedSymbols.size();
|
2016-07-28 06:55:30 +08:00
|
|
|
assert(symbolIndex >= numStabs && "Searched for stab via atomBySymbol?");
|
|
|
|
if (symbolIndex < numStabs+numLocal) {
|
|
|
|
sym = &normalizedFile.localSymbols[symbolIndex-numStabs];
|
|
|
|
} else if (symbolIndex < numStabs+numLocal+numGlobal) {
|
|
|
|
sym = &normalizedFile.globalSymbols[symbolIndex-numStabs-numLocal];
|
|
|
|
} else if (symbolIndex < numStabs+numLocal+numGlobal+numUndef) {
|
|
|
|
sym = &normalizedFile.undefinedSymbols[symbolIndex-numStabs-numLocal-
|
|
|
|
numGlobal];
|
2014-06-28 02:25:01 +08:00
|
|
|
} else {
|
2016-03-31 04:15:06 +08:00
|
|
|
return llvm::make_error<GenericError>(Twine("symbol index (")
|
2014-06-28 02:25:01 +08:00
|
|
|
+ Twine(symbolIndex) + ") out of range");
|
|
|
|
}
|
2016-07-28 06:55:30 +08:00
|
|
|
|
2014-06-28 02:25:01 +08:00
|
|
|
// Find atom from symbol.
|
|
|
|
if ((sym->type & N_TYPE) == N_SECT) {
|
|
|
|
if (sym->sect > normalizedFile.sections.size())
|
2016-03-31 04:15:06 +08:00
|
|
|
return llvm::make_error<GenericError>(Twine("symbol section index (")
|
2014-06-28 02:25:01 +08:00
|
|
|
+ Twine(sym->sect) + ") out of range ");
|
|
|
|
const Section &symSection = normalizedFile.sections[sym->sect-1];
|
|
|
|
uint64_t targetOffsetInSect = sym->value - symSection.address;
|
|
|
|
MachODefinedAtom *target = file.findAtomCoveringAddress(symSection,
|
|
|
|
targetOffsetInSect);
|
|
|
|
if (target) {
|
|
|
|
*result = target;
|
2016-11-11 12:29:25 +08:00
|
|
|
return llvm::Error::success();
|
2014-06-28 02:25:01 +08:00
|
|
|
}
|
2016-03-31 04:15:06 +08:00
|
|
|
return llvm::make_error<GenericError>("no atom found for defined symbol");
|
2014-06-28 02:25:01 +08:00
|
|
|
} else if ((sym->type & N_TYPE) == N_UNDF) {
|
|
|
|
const lld::Atom *target = file.findUndefAtom(sym->name);
|
|
|
|
if (target) {
|
|
|
|
*result = target;
|
2016-11-11 12:29:25 +08:00
|
|
|
return llvm::Error::success();
|
2014-06-28 02:25:01 +08:00
|
|
|
}
|
2016-03-31 04:15:06 +08:00
|
|
|
return llvm::make_error<GenericError>("no undefined atom found for sym");
|
2014-06-28 02:25:01 +08:00
|
|
|
} else {
|
|
|
|
// Search undefs
|
2016-03-31 04:15:06 +08:00
|
|
|
return llvm::make_error<GenericError>("no atom found for symbol");
|
2014-06-28 02:25:01 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-10-28 06:48:35 +08:00
|
|
|
const bool isBig = MachOLinkingContext::isBigEndian(normalizedFile.arch);
|
2014-06-28 02:25:01 +08:00
|
|
|
// Use old-school iterator so that paired relocations can be grouped.
|
|
|
|
for (auto it=section.relocations.begin(), e=section.relocations.end();
|
|
|
|
it != e; ++it) {
|
|
|
|
const Relocation &reloc = *it;
|
|
|
|
// Find atom this relocation is in.
|
|
|
|
if (reloc.offset > section.content.size())
|
2016-03-31 04:15:06 +08:00
|
|
|
return llvm::make_error<GenericError>(
|
|
|
|
Twine("r_address (") + Twine(reloc.offset)
|
2014-06-28 02:25:01 +08:00
|
|
|
+ ") is larger than section size ("
|
|
|
|
+ Twine(section.content.size()) + ")");
|
|
|
|
uint32_t offsetInAtom;
|
|
|
|
MachODefinedAtom *inAtom = file.findAtomCoveringAddress(section,
|
|
|
|
reloc.offset,
|
|
|
|
&offsetInAtom);
|
2014-07-01 04:44:47 +08:00
|
|
|
assert(inAtom && "r_address in range, should have found atom");
|
2014-06-28 02:25:01 +08:00
|
|
|
uint64_t fixupAddress = section.address + reloc.offset;
|
|
|
|
|
|
|
|
const lld::Atom *target = nullptr;
|
|
|
|
Reference::Addend addend = 0;
|
|
|
|
Reference::KindValue kind;
|
|
|
|
if (handler.isPairedReloc(reloc)) {
|
2015-12-11 02:48:52 +08:00
|
|
|
// Handle paired relocations together.
|
|
|
|
const Relocation &reloc2 = *++it;
|
2016-03-31 04:15:06 +08:00
|
|
|
auto relocErr = handler.getPairReferenceInfo(
|
2015-12-11 02:48:52 +08:00
|
|
|
reloc, reloc2, inAtom, offsetInAtom, fixupAddress, isBig, scatterable,
|
2014-10-28 06:48:35 +08:00
|
|
|
atomByAddr, atomBySymbol, &kind, &target, &addend);
|
2015-12-11 02:48:52 +08:00
|
|
|
if (relocErr) {
|
2016-03-31 04:15:06 +08:00
|
|
|
return handleErrors(std::move(relocErr),
|
|
|
|
[&](std::unique_ptr<GenericError> GE) {
|
|
|
|
return llvm::make_error<GenericError>(
|
|
|
|
Twine("bad relocation (") + GE->getMessage()
|
|
|
|
+ ") in section "
|
|
|
|
+ section.segmentName + "/" + section.sectionName
|
|
|
|
+ " (r1_address=" + Twine::utohexstr(reloc.offset)
|
|
|
|
+ ", r1_type=" + Twine(reloc.type)
|
|
|
|
+ ", r1_extern=" + Twine(reloc.isExtern)
|
|
|
|
+ ", r1_length=" + Twine((int)reloc.length)
|
|
|
|
+ ", r1_pcrel=" + Twine(reloc.pcRel)
|
|
|
|
+ (!reloc.scattered ? (Twine(", r1_symbolnum=")
|
|
|
|
+ Twine(reloc.symbol))
|
|
|
|
: (Twine(", r1_scattered=1, r1_value=")
|
|
|
|
+ Twine(reloc.value)))
|
|
|
|
+ ")"
|
|
|
|
+ ", (r2_address=" + Twine::utohexstr(reloc2.offset)
|
|
|
|
+ ", r2_type=" + Twine(reloc2.type)
|
|
|
|
+ ", r2_extern=" + Twine(reloc2.isExtern)
|
|
|
|
+ ", r2_length=" + Twine((int)reloc2.length)
|
|
|
|
+ ", r2_pcrel=" + Twine(reloc2.pcRel)
|
|
|
|
+ (!reloc2.scattered ? (Twine(", r2_symbolnum=")
|
|
|
|
+ Twine(reloc2.symbol))
|
|
|
|
: (Twine(", r2_scattered=1, r2_value=")
|
|
|
|
+ Twine(reloc2.value)))
|
|
|
|
+ ")" );
|
|
|
|
});
|
2015-12-11 02:48:52 +08:00
|
|
|
}
|
2014-06-28 02:25:01 +08:00
|
|
|
}
|
|
|
|
else {
|
2014-07-17 03:49:02 +08:00
|
|
|
// Use ArchHandler to convert relocation record into information
|
2014-06-28 02:25:01 +08:00
|
|
|
// needed to instantiate an lld::Reference object.
|
2016-03-31 04:15:06 +08:00
|
|
|
auto relocErr = handler.getReferenceInfo(
|
2014-10-28 06:48:35 +08:00
|
|
|
reloc, inAtom, offsetInAtom, fixupAddress, isBig, atomByAddr,
|
|
|
|
atomBySymbol, &kind, &target, &addend);
|
2015-12-11 02:48:52 +08:00
|
|
|
if (relocErr) {
|
2016-03-31 04:15:06 +08:00
|
|
|
return handleErrors(std::move(relocErr),
|
|
|
|
[&](std::unique_ptr<GenericError> GE) {
|
|
|
|
return llvm::make_error<GenericError>(
|
|
|
|
Twine("bad relocation (") + GE->getMessage()
|
|
|
|
+ ") in section "
|
|
|
|
+ section.segmentName + "/" + section.sectionName
|
|
|
|
+ " (r_address=" + Twine::utohexstr(reloc.offset)
|
|
|
|
+ ", r_type=" + Twine(reloc.type)
|
|
|
|
+ ", r_extern=" + Twine(reloc.isExtern)
|
|
|
|
+ ", r_length=" + Twine((int)reloc.length)
|
|
|
|
+ ", r_pcrel=" + Twine(reloc.pcRel)
|
|
|
|
+ (!reloc.scattered ? (Twine(", r_symbolnum=") + Twine(reloc.symbol))
|
|
|
|
: (Twine(", r_scattered=1, r_value=")
|
|
|
|
+ Twine(reloc.value)))
|
|
|
|
+ ")" );
|
|
|
|
});
|
2015-12-11 02:48:52 +08:00
|
|
|
}
|
2014-06-28 02:25:01 +08:00
|
|
|
}
|
2015-12-11 02:48:52 +08:00
|
|
|
// Instantiate an lld::Reference object and add to its atom.
|
2016-02-02 07:42:10 +08:00
|
|
|
inAtom->addReference(Reference::KindNamespace::mach_o,
|
|
|
|
handler.kindArch(),
|
|
|
|
kind, offsetInAtom, target, addend);
|
2014-06-28 02:25:01 +08:00
|
|
|
}
|
2014-07-23 08:51:37 +08:00
|
|
|
|
2016-11-11 12:29:25 +08:00
|
|
|
return llvm::Error::success();
|
2014-06-28 02:25:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool isDebugInfoSection(const Section §ion) {
|
|
|
|
if ((section.attributes & S_ATTR_DEBUG) == 0)
|
|
|
|
return false;
|
|
|
|
return section.segmentName.equals("__DWARF");
|
|
|
|
}
|
|
|
|
|
2016-07-28 06:55:30 +08:00
|
|
|
static const Atom* findDefinedAtomByName(MachOFile &file, Twine name) {
|
|
|
|
std::string strName = name.str();
|
|
|
|
for (auto *atom : file.defined())
|
|
|
|
if (atom->name() == strName)
|
|
|
|
return atom;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static StringRef copyDebugString(StringRef str, BumpPtrAllocator &alloc) {
|
2016-07-30 04:04:18 +08:00
|
|
|
char *strCopy = alloc.Allocate<char>(str.size() + 1);
|
|
|
|
memcpy(strCopy, str.data(), str.size());
|
|
|
|
strCopy[str.size()] = '\0';
|
|
|
|
return strCopy;
|
2016-07-28 06:55:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Error parseStabs(MachOFile &file,
|
|
|
|
const NormalizedFile &normalizedFile,
|
|
|
|
bool copyRefs) {
|
|
|
|
|
|
|
|
if (normalizedFile.stabsSymbols.empty())
|
|
|
|
return llvm::Error::success();
|
|
|
|
|
|
|
|
// FIXME: Kill this off when we can move to sane yaml parsing.
|
|
|
|
std::unique_ptr<BumpPtrAllocator> allocator;
|
|
|
|
if (copyRefs)
|
2019-08-15 06:28:17 +08:00
|
|
|
allocator = std::make_unique<BumpPtrAllocator>();
|
2016-07-28 06:55:30 +08:00
|
|
|
|
|
|
|
enum { start, inBeginEnd } state = start;
|
|
|
|
|
|
|
|
const Atom *currentAtom = nullptr;
|
|
|
|
uint64_t currentAtomAddress = 0;
|
|
|
|
StabsDebugInfo::StabsList stabsList;
|
|
|
|
for (const auto &stabSym : normalizedFile.stabsSymbols) {
|
|
|
|
Stab stab(nullptr, stabSym.type, stabSym.sect, stabSym.desc,
|
|
|
|
stabSym.value, stabSym.name);
|
|
|
|
switch (state) {
|
|
|
|
case start:
|
|
|
|
switch (static_cast<StabType>(stabSym.type)) {
|
|
|
|
case N_BNSYM:
|
|
|
|
state = inBeginEnd;
|
|
|
|
currentAtomAddress = stabSym.value;
|
|
|
|
Reference::Addend addend;
|
|
|
|
currentAtom = findAtomCoveringAddress(normalizedFile, file,
|
|
|
|
currentAtomAddress, addend);
|
|
|
|
if (addend != 0)
|
|
|
|
return llvm::make_error<GenericError>(
|
|
|
|
"Non-zero addend for BNSYM '" + stabSym.name + "' in " +
|
|
|
|
file.path());
|
|
|
|
if (currentAtom)
|
|
|
|
stab.atom = currentAtom;
|
|
|
|
else {
|
|
|
|
// FIXME: ld64 just issues a warning here - should we match that?
|
|
|
|
return llvm::make_error<GenericError>(
|
|
|
|
"can't find atom for stabs BNSYM at " +
|
|
|
|
Twine::utohexstr(stabSym.value) + " in " + file.path());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case N_SO:
|
|
|
|
case N_OSO:
|
|
|
|
// Not associated with an atom, just copy.
|
|
|
|
if (copyRefs)
|
|
|
|
stab.str = copyDebugString(stabSym.name, *allocator);
|
|
|
|
else
|
|
|
|
stab.str = stabSym.name;
|
|
|
|
break;
|
|
|
|
case N_GSYM: {
|
|
|
|
auto colonIdx = stabSym.name.find(':');
|
|
|
|
if (colonIdx != StringRef::npos) {
|
|
|
|
StringRef name = stabSym.name.substr(0, colonIdx);
|
|
|
|
currentAtom = findDefinedAtomByName(file, "_" + name);
|
|
|
|
stab.atom = currentAtom;
|
|
|
|
if (copyRefs)
|
|
|
|
stab.str = copyDebugString(stabSym.name, *allocator);
|
|
|
|
else
|
|
|
|
stab.str = stabSym.name;
|
|
|
|
} else {
|
|
|
|
currentAtom = findDefinedAtomByName(file, stabSym.name);
|
|
|
|
stab.atom = currentAtom;
|
|
|
|
if (copyRefs)
|
|
|
|
stab.str = copyDebugString(stabSym.name, *allocator);
|
|
|
|
else
|
|
|
|
stab.str = stabSym.name;
|
|
|
|
}
|
|
|
|
if (stab.atom == nullptr)
|
|
|
|
return llvm::make_error<GenericError>(
|
|
|
|
"can't find atom for N_GSYM stabs" + stabSym.name +
|
|
|
|
" in " + file.path());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case N_FUN:
|
|
|
|
return llvm::make_error<GenericError>(
|
|
|
|
"old-style N_FUN stab '" + stabSym.name + "' unsupported");
|
|
|
|
default:
|
|
|
|
return llvm::make_error<GenericError>(
|
|
|
|
"unrecognized stab symbol '" + stabSym.name + "'");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case inBeginEnd:
|
|
|
|
stab.atom = currentAtom;
|
|
|
|
switch (static_cast<StabType>(stabSym.type)) {
|
|
|
|
case N_ENSYM:
|
|
|
|
state = start;
|
|
|
|
currentAtom = nullptr;
|
|
|
|
break;
|
|
|
|
case N_FUN:
|
|
|
|
// Just copy the string.
|
|
|
|
if (copyRefs)
|
|
|
|
stab.str = copyDebugString(stabSym.name, *allocator);
|
|
|
|
else
|
|
|
|
stab.str = stabSym.name;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return llvm::make_error<GenericError>(
|
|
|
|
"unrecognized stab symbol '" + stabSym.name + "'");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
llvm::dbgs() << "Adding to stabsList: " << stab << "\n";
|
|
|
|
stabsList.push_back(stab);
|
|
|
|
}
|
|
|
|
|
2019-08-15 06:28:17 +08:00
|
|
|
file.setDebugInfo(std::make_unique<StabsDebugInfo>(std::move(stabsList)));
|
2016-07-28 06:55:30 +08:00
|
|
|
|
|
|
|
// FIXME: Kill this off when we fix YAML memory ownership.
|
|
|
|
file.debugInfo()->setAllocator(std::move(allocator));
|
|
|
|
|
|
|
|
return llvm::Error::success();
|
|
|
|
}
|
|
|
|
|
|
|
|
static llvm::DataExtractor
|
|
|
|
dataExtractorFromSection(const NormalizedFile &normalizedFile,
|
|
|
|
const Section &S) {
|
|
|
|
const bool is64 = MachOLinkingContext::is64Bit(normalizedFile.arch);
|
|
|
|
const bool isBig = MachOLinkingContext::isBigEndian(normalizedFile.arch);
|
|
|
|
StringRef SecData(reinterpret_cast<const char*>(S.content.data()),
|
|
|
|
S.content.size());
|
|
|
|
return llvm::DataExtractor(SecData, !isBig, is64 ? 8 : 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Cribbed from llvm-dwp -- should share "lightweight CU DIE
|
|
|
|
// inspection" code if possible.
|
2019-08-06 21:37:34 +08:00
|
|
|
static uint64_t getCUAbbrevOffset(llvm::DataExtractor abbrevData,
|
2016-07-28 06:55:30 +08:00
|
|
|
uint64_t abbrCode) {
|
2019-08-06 21:37:34 +08:00
|
|
|
uint64_t offset = 0;
|
2021-04-27 13:09:10 +08:00
|
|
|
while (abbrevData.getULEB128(&offset) != abbrCode) {
|
2016-07-28 06:55:30 +08:00
|
|
|
// Tag
|
|
|
|
abbrevData.getULEB128(&offset);
|
|
|
|
// DW_CHILDREN
|
|
|
|
abbrevData.getU8(&offset);
|
|
|
|
// Attributes
|
|
|
|
while (abbrevData.getULEB128(&offset) | abbrevData.getULEB128(&offset))
|
|
|
|
;
|
|
|
|
}
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Cribbed from llvm-dwp -- should share "lightweight CU DIE
|
|
|
|
// inspection" code if possible.
|
|
|
|
static Expected<const char *>
|
|
|
|
getIndexedString(const NormalizedFile &normalizedFile,
|
2016-10-28 01:43:39 +08:00
|
|
|
llvm::dwarf::Form form, llvm::DataExtractor infoData,
|
2019-08-06 21:37:34 +08:00
|
|
|
uint64_t &infoOffset, const Section &stringsSection) {
|
2016-07-28 06:55:30 +08:00
|
|
|
if (form == llvm::dwarf::DW_FORM_string)
|
|
|
|
return infoData.getCStr(&infoOffset);
|
|
|
|
if (form != llvm::dwarf::DW_FORM_strp)
|
|
|
|
return llvm::make_error<GenericError>(
|
|
|
|
"string field encoded without DW_FORM_strp");
|
2019-08-06 21:37:34 +08:00
|
|
|
uint64_t stringOffset = infoData.getU32(&infoOffset);
|
2016-07-28 06:55:30 +08:00
|
|
|
llvm::DataExtractor stringsData =
|
|
|
|
dataExtractorFromSection(normalizedFile, stringsSection);
|
|
|
|
return stringsData.getCStr(&stringOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Cribbed from llvm-dwp -- should share "lightweight CU DIE
|
|
|
|
// inspection" code if possible.
|
|
|
|
static llvm::Expected<TranslationUnitSource>
|
|
|
|
readCompUnit(const NormalizedFile &normalizedFile,
|
|
|
|
const Section &info,
|
|
|
|
const Section &abbrev,
|
|
|
|
const Section &strings,
|
|
|
|
StringRef path) {
|
|
|
|
// FIXME: Cribbed from llvm-dwp -- should share "lightweight CU DIE
|
|
|
|
// inspection" code if possible.
|
2019-08-06 21:37:34 +08:00
|
|
|
uint64_t offset = 0;
|
2016-11-12 00:23:47 +08:00
|
|
|
llvm::dwarf::DwarfFormat Format = llvm::dwarf::DwarfFormat::DWARF32;
|
2016-07-28 06:55:30 +08:00
|
|
|
auto infoData = dataExtractorFromSection(normalizedFile, info);
|
|
|
|
uint32_t length = infoData.getU32(&offset);
|
2019-12-16 22:16:14 +08:00
|
|
|
if (length == llvm::dwarf::DW_LENGTH_DWARF64) {
|
2016-11-12 00:23:47 +08:00
|
|
|
Format = llvm::dwarf::DwarfFormat::DWARF64;
|
2016-07-28 06:55:30 +08:00
|
|
|
infoData.getU64(&offset);
|
2016-11-12 00:23:47 +08:00
|
|
|
}
|
2019-12-16 22:16:14 +08:00
|
|
|
else if (length >= llvm::dwarf::DW_LENGTH_lo_reserved)
|
2016-07-28 06:55:30 +08:00
|
|
|
return llvm::make_error<GenericError>("Malformed DWARF in " + path);
|
|
|
|
|
|
|
|
uint16_t version = infoData.getU16(&offset);
|
|
|
|
|
|
|
|
if (version < 2 || version > 4)
|
|
|
|
return llvm::make_error<GenericError>("Unsupported DWARF version in " +
|
|
|
|
path);
|
|
|
|
|
|
|
|
infoData.getU32(&offset); // Abbrev offset (should be zero)
|
|
|
|
uint8_t addrSize = infoData.getU8(&offset);
|
|
|
|
|
|
|
|
uint32_t abbrCode = infoData.getULEB128(&offset);
|
|
|
|
auto abbrevData = dataExtractorFromSection(normalizedFile, abbrev);
|
2019-08-06 21:37:34 +08:00
|
|
|
uint64_t abbrevOffset = getCUAbbrevOffset(abbrevData, abbrCode);
|
2016-07-28 06:55:30 +08:00
|
|
|
uint64_t tag = abbrevData.getULEB128(&abbrevOffset);
|
|
|
|
if (tag != llvm::dwarf::DW_TAG_compile_unit)
|
|
|
|
return llvm::make_error<GenericError>("top level DIE is not a compile unit");
|
|
|
|
// DW_CHILDREN
|
|
|
|
abbrevData.getU8(&abbrevOffset);
|
|
|
|
uint32_t name;
|
2016-10-28 01:43:39 +08:00
|
|
|
llvm::dwarf::Form form;
|
2018-03-14 17:57:52 +08:00
|
|
|
llvm::dwarf::FormParams formParams = {version, addrSize, Format};
|
2016-07-28 06:55:30 +08:00
|
|
|
TranslationUnitSource tu;
|
|
|
|
while ((name = abbrevData.getULEB128(&abbrevOffset)) |
|
2016-10-28 01:43:39 +08:00
|
|
|
(form = static_cast<llvm::dwarf::Form>(
|
|
|
|
abbrevData.getULEB128(&abbrevOffset))) &&
|
2016-07-28 06:55:30 +08:00
|
|
|
(name != 0 || form != 0)) {
|
|
|
|
switch (name) {
|
|
|
|
case llvm::dwarf::DW_AT_name: {
|
|
|
|
if (auto eName = getIndexedString(normalizedFile, form, infoData, offset,
|
|
|
|
strings))
|
|
|
|
tu.name = *eName;
|
|
|
|
else
|
|
|
|
return eName.takeError();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case llvm::dwarf::DW_AT_comp_dir: {
|
|
|
|
if (auto eName = getIndexedString(normalizedFile, form, infoData, offset,
|
|
|
|
strings))
|
|
|
|
tu.path = *eName;
|
|
|
|
else
|
|
|
|
return eName.takeError();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2017-06-27 02:43:26 +08:00
|
|
|
llvm::DWARFFormValue::skipValue(form, infoData, &offset, formParams);
|
2016-07-28 06:55:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return tu;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Error parseDebugInfo(MachOFile &file,
|
|
|
|
const NormalizedFile &normalizedFile, bool copyRefs) {
|
|
|
|
|
|
|
|
// Find the interesting debug info sections.
|
|
|
|
const Section *debugInfo = nullptr;
|
|
|
|
const Section *debugAbbrev = nullptr;
|
|
|
|
const Section *debugStrings = nullptr;
|
|
|
|
|
|
|
|
for (auto &s : normalizedFile.sections) {
|
|
|
|
if (s.segmentName == "__DWARF") {
|
|
|
|
if (s.sectionName == "__debug_info")
|
|
|
|
debugInfo = &s;
|
|
|
|
else if (s.sectionName == "__debug_abbrev")
|
|
|
|
debugAbbrev = &s;
|
|
|
|
else if (s.sectionName == "__debug_str")
|
|
|
|
debugStrings = &s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!debugInfo)
|
|
|
|
return parseStabs(file, normalizedFile, copyRefs);
|
|
|
|
|
|
|
|
if (debugInfo->content.size() == 0)
|
|
|
|
return llvm::Error::success();
|
|
|
|
|
|
|
|
if (debugInfo->content.size() < 12)
|
|
|
|
return llvm::make_error<GenericError>("Malformed __debug_info section in " +
|
|
|
|
file.path() + ": too small");
|
|
|
|
|
|
|
|
if (!debugAbbrev)
|
|
|
|
return llvm::make_error<GenericError>("Missing __dwarf_abbrev section in " +
|
|
|
|
file.path());
|
|
|
|
|
|
|
|
if (auto tuOrErr = readCompUnit(normalizedFile, *debugInfo, *debugAbbrev,
|
|
|
|
*debugStrings, file.path())) {
|
|
|
|
// FIXME: Kill of allocator and code under 'copyRefs' when we fix YAML
|
|
|
|
// memory ownership.
|
|
|
|
std::unique_ptr<BumpPtrAllocator> allocator;
|
|
|
|
if (copyRefs) {
|
2019-08-15 06:28:17 +08:00
|
|
|
allocator = std::make_unique<BumpPtrAllocator>();
|
2016-07-28 06:55:30 +08:00
|
|
|
tuOrErr->name = copyDebugString(tuOrErr->name, *allocator);
|
|
|
|
tuOrErr->path = copyDebugString(tuOrErr->path, *allocator);
|
|
|
|
}
|
2019-08-15 06:28:17 +08:00
|
|
|
file.setDebugInfo(std::make_unique<DwarfDebugInfo>(std::move(*tuOrErr)));
|
2016-07-28 06:55:30 +08:00
|
|
|
if (copyRefs)
|
|
|
|
file.debugInfo()->setAllocator(std::move(allocator));
|
|
|
|
} else
|
|
|
|
return tuOrErr.takeError();
|
|
|
|
|
|
|
|
return llvm::Error::success();
|
|
|
|
}
|
|
|
|
|
2014-10-28 06:48:35 +08:00
|
|
|
static int64_t readSPtr(bool is64, bool isBig, const uint8_t *addr) {
|
2014-10-16 02:19:31 +08:00
|
|
|
if (is64)
|
2014-10-28 06:48:35 +08:00
|
|
|
return read64(addr, isBig);
|
2014-10-16 02:19:31 +08:00
|
|
|
|
2014-10-28 06:48:35 +08:00
|
|
|
int32_t res = read32(addr, isBig);
|
2014-10-16 02:19:31 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2015-05-13 08:44:47 +08:00
|
|
|
/// --- Augmentation String Processing ---
|
|
|
|
|
2015-05-16 08:08:02 +08:00
|
|
|
struct CIEInfo {
|
|
|
|
bool _augmentationDataPresent = false;
|
2016-03-16 05:33:10 +08:00
|
|
|
bool _mayHaveEH = false;
|
|
|
|
uint32_t _offsetOfLSDA = ~0U;
|
|
|
|
uint32_t _offsetOfPersonality = ~0U;
|
|
|
|
uint32_t _offsetOfFDEPointerEncoding = ~0U;
|
|
|
|
uint32_t _augmentationDataLength = ~0U;
|
2015-05-13 08:44:47 +08:00
|
|
|
};
|
|
|
|
|
2015-05-16 08:08:02 +08:00
|
|
|
typedef llvm::DenseMap<const MachODefinedAtom*, CIEInfo> CIEInfoMap;
|
|
|
|
|
2016-03-31 07:43:27 +08:00
|
|
|
static llvm::Error processAugmentationString(const uint8_t *augStr,
|
|
|
|
CIEInfo &cieInfo,
|
|
|
|
unsigned &len) {
|
2015-05-13 08:44:47 +08:00
|
|
|
|
|
|
|
if (augStr[0] == '\0') {
|
2016-03-16 05:33:10 +08:00
|
|
|
len = 1;
|
2016-11-11 12:29:25 +08:00
|
|
|
return llvm::Error::success();
|
2015-05-13 08:44:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (augStr[0] != 'z')
|
2016-03-31 07:43:27 +08:00
|
|
|
return llvm::make_error<GenericError>("expected 'z' at start of "
|
|
|
|
"augmentation string");
|
2015-05-13 08:44:47 +08:00
|
|
|
|
2015-05-16 08:08:02 +08:00
|
|
|
cieInfo._augmentationDataPresent = true;
|
2015-05-13 08:44:47 +08:00
|
|
|
uint64_t idx = 1;
|
|
|
|
|
2016-03-16 05:33:10 +08:00
|
|
|
uint32_t offsetInAugmentationData = 0;
|
2015-05-13 08:44:47 +08:00
|
|
|
while (augStr[idx] != '\0') {
|
|
|
|
if (augStr[idx] == 'L') {
|
2016-03-16 05:33:10 +08:00
|
|
|
cieInfo._offsetOfLSDA = offsetInAugmentationData;
|
|
|
|
// This adds a single byte to the augmentation data.
|
|
|
|
++offsetInAugmentationData;
|
2015-05-13 08:44:47 +08:00
|
|
|
++idx;
|
2016-03-16 05:33:10 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (augStr[idx] == 'P') {
|
|
|
|
cieInfo._offsetOfPersonality = offsetInAugmentationData;
|
|
|
|
// This adds a single byte to the augmentation data for the encoding,
|
|
|
|
// then a number of bytes for the pointer data.
|
|
|
|
// FIXME: We are assuming 4 is correct here for the pointer size as we
|
|
|
|
// always currently use delta32ToGOT.
|
|
|
|
offsetInAugmentationData += 5;
|
|
|
|
++idx;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (augStr[idx] == 'R') {
|
|
|
|
cieInfo._offsetOfFDEPointerEncoding = offsetInAugmentationData;
|
|
|
|
// This adds a single byte to the augmentation data.
|
|
|
|
++offsetInAugmentationData;
|
2015-05-13 08:44:47 +08:00
|
|
|
++idx;
|
2016-03-16 05:33:10 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (augStr[idx] == 'e') {
|
|
|
|
if (augStr[idx + 1] != 'h')
|
2016-03-31 07:43:27 +08:00
|
|
|
return llvm::make_error<GenericError>("expected 'eh' in "
|
|
|
|
"augmentation string");
|
2016-03-16 05:33:10 +08:00
|
|
|
cieInfo._mayHaveEH = true;
|
|
|
|
idx += 2;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
++idx;
|
2015-05-13 08:44:47 +08:00
|
|
|
}
|
|
|
|
|
2016-03-16 05:33:10 +08:00
|
|
|
cieInfo._augmentationDataLength = offsetInAugmentationData;
|
|
|
|
|
|
|
|
len = idx + 1;
|
2016-11-11 12:29:25 +08:00
|
|
|
return llvm::Error::success();
|
2015-05-13 08:44:47 +08:00
|
|
|
}
|
|
|
|
|
2016-03-31 07:43:27 +08:00
|
|
|
static llvm::Error processCIE(const NormalizedFile &normalizedFile,
|
|
|
|
MachOFile &file,
|
|
|
|
mach_o::ArchHandler &handler,
|
|
|
|
const Section *ehFrameSection,
|
|
|
|
MachODefinedAtom *atom,
|
|
|
|
uint64_t offset,
|
|
|
|
CIEInfoMap &cieInfos) {
|
2015-05-13 08:44:47 +08:00
|
|
|
const bool isBig = MachOLinkingContext::isBigEndian(normalizedFile.arch);
|
|
|
|
const uint8_t *frameData = atom->rawContent().data();
|
2015-05-16 08:08:02 +08:00
|
|
|
|
|
|
|
CIEInfo cieInfo;
|
|
|
|
|
2015-05-13 08:44:47 +08:00
|
|
|
uint32_t size = read32(frameData, isBig);
|
|
|
|
uint64_t cieIDField = size == 0xffffffffU
|
|
|
|
? sizeof(uint32_t) + sizeof(uint64_t)
|
|
|
|
: sizeof(uint32_t);
|
|
|
|
uint64_t versionField = cieIDField + sizeof(uint32_t);
|
|
|
|
uint64_t augmentationStringField = versionField + sizeof(uint8_t);
|
|
|
|
|
2016-03-16 05:33:10 +08:00
|
|
|
unsigned augmentationStringLength = 0;
|
2015-05-13 08:44:47 +08:00
|
|
|
if (auto err = processAugmentationString(frameData + augmentationStringField,
|
2016-03-16 05:33:10 +08:00
|
|
|
cieInfo, augmentationStringLength))
|
2015-05-13 08:44:47 +08:00
|
|
|
return err;
|
|
|
|
|
2016-03-16 05:33:10 +08:00
|
|
|
if (cieInfo._offsetOfPersonality != ~0U) {
|
|
|
|
// If we have augmentation data for the personality function, then we may
|
|
|
|
// need to implicitly generate its relocation.
|
|
|
|
|
|
|
|
// Parse the EH Data field which is pointer sized.
|
|
|
|
uint64_t EHDataField = augmentationStringField + augmentationStringLength;
|
|
|
|
const bool is64 = MachOLinkingContext::is64Bit(normalizedFile.arch);
|
|
|
|
unsigned EHDataFieldSize = (cieInfo._mayHaveEH ? (is64 ? 8 : 4) : 0);
|
|
|
|
|
|
|
|
// Parse Code Align Factor which is a ULEB128.
|
|
|
|
uint64_t CodeAlignField = EHDataField + EHDataFieldSize;
|
|
|
|
unsigned lengthFieldSize = 0;
|
|
|
|
llvm::decodeULEB128(frameData + CodeAlignField, &lengthFieldSize);
|
|
|
|
|
|
|
|
// Parse Data Align Factor which is a SLEB128.
|
|
|
|
uint64_t DataAlignField = CodeAlignField + lengthFieldSize;
|
|
|
|
llvm::decodeSLEB128(frameData + DataAlignField, &lengthFieldSize);
|
|
|
|
|
|
|
|
// Parse Return Address Register which is a byte.
|
|
|
|
uint64_t ReturnAddressField = DataAlignField + lengthFieldSize;
|
|
|
|
|
|
|
|
// Parse the augmentation length which is a ULEB128.
|
|
|
|
uint64_t AugmentationLengthField = ReturnAddressField + 1;
|
|
|
|
uint64_t AugmentationLength =
|
|
|
|
llvm::decodeULEB128(frameData + AugmentationLengthField,
|
|
|
|
&lengthFieldSize);
|
|
|
|
|
|
|
|
if (AugmentationLength != cieInfo._augmentationDataLength)
|
2016-03-31 07:43:27 +08:00
|
|
|
return llvm::make_error<GenericError>("CIE augmentation data length "
|
|
|
|
"mismatch");
|
2016-03-16 05:33:10 +08:00
|
|
|
|
|
|
|
// Get the start address of the augmentation data.
|
|
|
|
uint64_t AugmentationDataField = AugmentationLengthField + lengthFieldSize;
|
|
|
|
|
|
|
|
// Parse the personality function from the augmentation data.
|
|
|
|
uint64_t PersonalityField =
|
|
|
|
AugmentationDataField + cieInfo._offsetOfPersonality;
|
|
|
|
|
|
|
|
// Parse the personality encoding.
|
|
|
|
// FIXME: Verify that this is a 32-bit pcrel offset.
|
|
|
|
uint64_t PersonalityFunctionField = PersonalityField + 1;
|
|
|
|
|
|
|
|
if (atom->begin() != atom->end()) {
|
|
|
|
// If we have an explicit relocation, then make sure it matches this
|
|
|
|
// offset as this is where we'd expect it to be applied to.
|
|
|
|
DefinedAtom::reference_iterator CurrentRef = atom->begin();
|
|
|
|
if (CurrentRef->offsetInAtom() != PersonalityFunctionField)
|
2016-03-31 07:43:27 +08:00
|
|
|
return llvm::make_error<GenericError>("CIE personality reloc at "
|
|
|
|
"wrong offset");
|
2016-03-16 05:33:10 +08:00
|
|
|
|
|
|
|
if (++CurrentRef != atom->end())
|
2016-03-31 07:43:27 +08:00
|
|
|
return llvm::make_error<GenericError>("CIE contains too many relocs");
|
2016-03-16 05:33:10 +08:00
|
|
|
} else {
|
|
|
|
// Implicitly generate the personality function reloc. It's assumed to
|
|
|
|
// be a delta32 offset to a GOT entry.
|
|
|
|
// FIXME: Parse the encoding and check this.
|
|
|
|
int32_t funcDelta = read32(frameData + PersonalityFunctionField, isBig);
|
|
|
|
uint64_t funcAddress = ehFrameSection->address + offset +
|
|
|
|
PersonalityFunctionField;
|
|
|
|
funcAddress += funcDelta;
|
|
|
|
|
|
|
|
const MachODefinedAtom *func = nullptr;
|
|
|
|
Reference::Addend addend;
|
|
|
|
func = findAtomCoveringAddress(normalizedFile, file, funcAddress,
|
2016-07-28 06:55:30 +08:00
|
|
|
addend);
|
2016-03-16 05:33:10 +08:00
|
|
|
atom->addReference(Reference::KindNamespace::mach_o, handler.kindArch(),
|
|
|
|
handler.unwindRefToPersonalityFunctionKind(),
|
|
|
|
PersonalityFunctionField, func, addend);
|
|
|
|
}
|
|
|
|
} else if (atom->begin() != atom->end()) {
|
|
|
|
// Otherwise, we expect there to be no relocations in this atom as the only
|
|
|
|
// relocation would have been to the personality function.
|
2016-03-31 07:43:27 +08:00
|
|
|
return llvm::make_error<GenericError>("unexpected relocation in CIE");
|
2016-03-16 05:33:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-16 08:08:02 +08:00
|
|
|
cieInfos[atom] = std::move(cieInfo);
|
|
|
|
|
2016-11-11 12:29:25 +08:00
|
|
|
return llvm::Error::success();
|
2015-05-13 08:44:47 +08:00
|
|
|
}
|
|
|
|
|
2016-03-31 07:43:27 +08:00
|
|
|
static llvm::Error processFDE(const NormalizedFile &normalizedFile,
|
|
|
|
MachOFile &file,
|
|
|
|
mach_o::ArchHandler &handler,
|
|
|
|
const Section *ehFrameSection,
|
|
|
|
MachODefinedAtom *atom,
|
|
|
|
uint64_t offset,
|
|
|
|
const CIEInfoMap &cieInfos) {
|
2015-05-13 08:44:47 +08:00
|
|
|
|
|
|
|
const bool isBig = MachOLinkingContext::isBigEndian(normalizedFile.arch);
|
|
|
|
const bool is64 = MachOLinkingContext::is64Bit(normalizedFile.arch);
|
|
|
|
|
|
|
|
// Compiler wasn't lazy and actually told us what it meant.
|
2016-03-16 05:33:10 +08:00
|
|
|
// Unfortunately, the compiler may not have generated references for all of
|
|
|
|
// [cie, func, lsda] and so we still need to parse the FDE and add references
|
|
|
|
// for any the compiler didn't generate.
|
2015-05-13 08:44:47 +08:00
|
|
|
if (atom->begin() != atom->end())
|
2016-03-16 05:33:10 +08:00
|
|
|
atom->sortReferences();
|
|
|
|
|
|
|
|
DefinedAtom::reference_iterator CurrentRef = atom->begin();
|
|
|
|
|
|
|
|
// This helper returns the reference (if one exists) at the offset we are
|
|
|
|
// currently processing. It automatically increments the ref iterator if we
|
|
|
|
// do return a ref, and throws an error if we pass over a ref without
|
|
|
|
// comsuming it.
|
|
|
|
auto currentRefGetter = [&CurrentRef,
|
|
|
|
&atom](uint64_t Offset)->const Reference* {
|
|
|
|
// If there are no more refs found, then we are done.
|
|
|
|
if (CurrentRef == atom->end())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
const Reference *Ref = *CurrentRef;
|
|
|
|
|
|
|
|
// If we haven't reached the offset for this reference, then return that
|
|
|
|
// we don't yet have a reference to process.
|
|
|
|
if (Offset < Ref->offsetInAtom())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// If the offset is equal, then we want to process this ref.
|
|
|
|
if (Offset == Ref->offsetInAtom()) {
|
|
|
|
++CurrentRef;
|
|
|
|
return Ref;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The current ref is at an offset which is earlier than the current
|
|
|
|
// offset, then we failed to consume it when we should have. In this case
|
|
|
|
// throw an error.
|
|
|
|
llvm::report_fatal_error("Skipped reference when processing FDE");
|
|
|
|
};
|
|
|
|
|
|
|
|
// Helper to either get the reference at this current location, and verify
|
|
|
|
// that it is of the expected type, or add a reference of that type.
|
|
|
|
// Returns the reference target.
|
|
|
|
auto verifyOrAddReference = [&](uint64_t targetAddress,
|
|
|
|
Reference::KindValue refKind,
|
|
|
|
uint64_t refAddress,
|
|
|
|
bool allowsAddend)->const Atom* {
|
|
|
|
if (auto *ref = currentRefGetter(refAddress)) {
|
|
|
|
// The compiler already emitted a relocation for the CIE ref. This should
|
|
|
|
// have been converted to the correct type of reference in
|
|
|
|
// get[Pair]ReferenceInfo().
|
|
|
|
assert(ref->kindValue() == refKind &&
|
|
|
|
"Incorrect EHFrame reference kind");
|
|
|
|
return ref->target();
|
|
|
|
}
|
|
|
|
Reference::Addend addend;
|
|
|
|
auto *target = findAtomCoveringAddress(normalizedFile, file,
|
2016-07-28 06:55:30 +08:00
|
|
|
targetAddress, addend);
|
2016-03-16 05:33:10 +08:00
|
|
|
atom->addReference(Reference::KindNamespace::mach_o, handler.kindArch(),
|
|
|
|
refKind, refAddress, target, addend);
|
|
|
|
|
|
|
|
if (!allowsAddend)
|
|
|
|
assert(!addend && "EHFrame reference cannot have addend");
|
|
|
|
return target;
|
|
|
|
};
|
|
|
|
|
|
|
|
const uint8_t *startFrameData = atom->rawContent().data();
|
|
|
|
const uint8_t *frameData = startFrameData;
|
2015-05-13 08:44:47 +08:00
|
|
|
|
|
|
|
uint32_t size = read32(frameData, isBig);
|
|
|
|
uint64_t cieFieldInFDE = size == 0xffffffffU
|
|
|
|
? sizeof(uint32_t) + sizeof(uint64_t)
|
|
|
|
: sizeof(uint32_t);
|
|
|
|
|
|
|
|
// Linker needs to fixup a reference from the FDE to its parent CIE (a
|
|
|
|
// 32-bit byte offset backwards in the __eh_frame section).
|
|
|
|
uint32_t cieDelta = read32(frameData + cieFieldInFDE, isBig);
|
|
|
|
uint64_t cieAddress = ehFrameSection->address + offset + cieFieldInFDE;
|
|
|
|
cieAddress -= cieDelta;
|
|
|
|
|
2016-03-16 05:33:10 +08:00
|
|
|
auto *cieRefTarget = verifyOrAddReference(cieAddress,
|
|
|
|
handler.unwindRefToCIEKind(),
|
|
|
|
cieFieldInFDE, false);
|
|
|
|
const MachODefinedAtom *cie = dyn_cast<MachODefinedAtom>(cieRefTarget);
|
|
|
|
assert(cie && cie->contentType() == DefinedAtom::typeCFI &&
|
2015-05-16 08:08:02 +08:00
|
|
|
"FDE's CIE field does not point at the start of a CIE.");
|
|
|
|
|
|
|
|
const CIEInfo &cieInfo = cieInfos.find(cie)->second;
|
|
|
|
|
2015-05-13 08:44:47 +08:00
|
|
|
// Linker needs to fixup reference from the FDE to the function it's
|
|
|
|
// describing. FIXME: there are actually different ways to do this, and the
|
|
|
|
// particular method used is specified in the CIE's augmentation fields
|
|
|
|
// (hopefully)
|
|
|
|
uint64_t rangeFieldInFDE = cieFieldInFDE + sizeof(uint32_t);
|
|
|
|
|
|
|
|
int64_t functionFromFDE = readSPtr(is64, isBig,
|
|
|
|
frameData + rangeFieldInFDE);
|
|
|
|
uint64_t rangeStart = ehFrameSection->address + offset + rangeFieldInFDE;
|
|
|
|
rangeStart += functionFromFDE;
|
|
|
|
|
2016-03-16 05:33:10 +08:00
|
|
|
verifyOrAddReference(rangeStart,
|
|
|
|
handler.unwindRefToFunctionKind(),
|
|
|
|
rangeFieldInFDE, true);
|
2015-05-13 08:44:47 +08:00
|
|
|
|
|
|
|
// Handle the augmentation data if there is any.
|
2015-05-16 08:08:02 +08:00
|
|
|
if (cieInfo._augmentationDataPresent) {
|
2015-05-13 08:44:47 +08:00
|
|
|
// First process the augmentation data length field.
|
|
|
|
uint64_t augmentationDataLengthFieldInFDE =
|
|
|
|
rangeFieldInFDE + 2 * (is64 ? sizeof(uint64_t) : sizeof(uint32_t));
|
|
|
|
unsigned lengthFieldSize = 0;
|
|
|
|
uint64_t augmentationDataLength =
|
|
|
|
llvm::decodeULEB128(frameData + augmentationDataLengthFieldInFDE,
|
|
|
|
&lengthFieldSize);
|
|
|
|
|
2016-03-16 05:33:10 +08:00
|
|
|
if (cieInfo._offsetOfLSDA != ~0U && augmentationDataLength > 0) {
|
2015-05-13 08:44:47 +08:00
|
|
|
|
|
|
|
// Look at the augmentation data field.
|
|
|
|
uint64_t augmentationDataFieldInFDE =
|
|
|
|
augmentationDataLengthFieldInFDE + lengthFieldSize;
|
|
|
|
|
|
|
|
int64_t lsdaFromFDE = readSPtr(is64, isBig,
|
|
|
|
frameData + augmentationDataFieldInFDE);
|
|
|
|
uint64_t lsdaStart =
|
|
|
|
ehFrameSection->address + offset + augmentationDataFieldInFDE +
|
|
|
|
lsdaFromFDE;
|
2016-03-16 05:33:10 +08:00
|
|
|
|
|
|
|
verifyOrAddReference(lsdaStart,
|
|
|
|
handler.unwindRefToFunctionKind(),
|
|
|
|
augmentationDataFieldInFDE, true);
|
2015-05-13 08:44:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-11 12:29:25 +08:00
|
|
|
return llvm::Error::success();
|
2015-05-13 08:44:47 +08:00
|
|
|
}
|
|
|
|
|
2016-03-31 07:43:27 +08:00
|
|
|
llvm::Error addEHFrameReferences(const NormalizedFile &normalizedFile,
|
|
|
|
MachOFile &file,
|
|
|
|
mach_o::ArchHandler &handler) {
|
2014-10-16 02:19:31 +08:00
|
|
|
|
|
|
|
const Section *ehFrameSection = nullptr;
|
|
|
|
for (auto §ion : normalizedFile.sections)
|
|
|
|
if (section.segmentName == "__TEXT" &&
|
|
|
|
section.sectionName == "__eh_frame") {
|
|
|
|
ehFrameSection = §ion;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No __eh_frame so nothing to do.
|
|
|
|
if (!ehFrameSection)
|
2016-11-11 12:29:25 +08:00
|
|
|
return llvm::Error::success();
|
2014-10-16 02:19:31 +08:00
|
|
|
|
2016-11-11 12:29:25 +08:00
|
|
|
llvm::Error ehFrameErr = llvm::Error::success();
|
2015-05-16 08:08:02 +08:00
|
|
|
CIEInfoMap cieInfos;
|
2015-05-13 08:44:47 +08:00
|
|
|
|
2014-10-16 02:19:31 +08:00
|
|
|
file.eachAtomInSection(*ehFrameSection,
|
|
|
|
[&](MachODefinedAtom *atom, uint64_t offset) -> void {
|
|
|
|
assert(atom->contentType() == DefinedAtom::typeCFI);
|
|
|
|
|
2015-05-13 08:44:47 +08:00
|
|
|
// Bail out if we've encountered an error.
|
|
|
|
if (ehFrameErr)
|
2014-10-16 02:19:31 +08:00
|
|
|
return;
|
|
|
|
|
2015-05-13 08:44:47 +08:00
|
|
|
const bool isBig = MachOLinkingContext::isBigEndian(normalizedFile.arch);
|
2015-05-16 08:08:02 +08:00
|
|
|
if (ArchHandler::isDwarfCIE(isBig, atom))
|
2016-03-16 05:33:10 +08:00
|
|
|
ehFrameErr = processCIE(normalizedFile, file, handler, ehFrameSection,
|
|
|
|
atom, offset, cieInfos);
|
2015-05-16 08:08:02 +08:00
|
|
|
else
|
2015-05-13 08:44:47 +08:00
|
|
|
ehFrameErr = processFDE(normalizedFile, file, handler, ehFrameSection,
|
2015-05-16 08:08:02 +08:00
|
|
|
atom, offset, cieInfos);
|
2014-10-16 02:19:31 +08:00
|
|
|
});
|
2015-05-13 08:44:47 +08:00
|
|
|
|
|
|
|
return ehFrameErr;
|
2014-10-16 02:19:31 +08:00
|
|
|
}
|
|
|
|
|
2016-03-31 07:43:27 +08:00
|
|
|
llvm::Error parseObjCImageInfo(const Section §,
|
|
|
|
const NormalizedFile &normalizedFile,
|
|
|
|
MachOFile &file) {
|
2016-01-15 07:35:05 +08:00
|
|
|
|
|
|
|
// struct objc_image_info {
|
|
|
|
// uint32_t version; // initially 0
|
|
|
|
// uint32_t flags;
|
|
|
|
// };
|
2016-01-16 08:57:07 +08:00
|
|
|
|
2016-01-20 05:54:21 +08:00
|
|
|
ArrayRef<uint8_t> content = sect.content;
|
2016-01-15 07:35:05 +08:00
|
|
|
if (content.size() != 8)
|
2016-03-31 07:43:27 +08:00
|
|
|
return llvm::make_error<GenericError>(sect.segmentName + "/" +
|
|
|
|
sect.sectionName +
|
|
|
|
" in file " + file.path() +
|
|
|
|
" should be 8 bytes in size");
|
2016-01-15 07:35:05 +08:00
|
|
|
|
2016-01-16 07:25:37 +08:00
|
|
|
const bool isBig = MachOLinkingContext::isBigEndian(normalizedFile.arch);
|
|
|
|
uint32_t version = read32(content.data(), isBig);
|
|
|
|
if (version)
|
2016-03-31 07:43:27 +08:00
|
|
|
return llvm::make_error<GenericError>(sect.segmentName + "/" +
|
|
|
|
sect.sectionName +
|
|
|
|
" in file " + file.path() +
|
|
|
|
" should have version=0");
|
2016-01-16 07:25:37 +08:00
|
|
|
|
2016-01-16 08:07:22 +08:00
|
|
|
uint32_t flags = read32(content.data() + 4, isBig);
|
2016-01-20 02:46:40 +08:00
|
|
|
if (flags & (MachOLinkingContext::objc_supports_gc |
|
|
|
|
MachOLinkingContext::objc_gc_only))
|
2016-03-31 07:43:27 +08:00
|
|
|
return llvm::make_error<GenericError>(sect.segmentName + "/" +
|
|
|
|
sect.sectionName +
|
|
|
|
" in file " + file.path() +
|
|
|
|
" uses GC. This is not supported");
|
2016-01-16 08:57:07 +08:00
|
|
|
|
2016-01-20 02:46:40 +08:00
|
|
|
if (flags & MachOLinkingContext::objc_retainReleaseForSimulator)
|
|
|
|
file.setObjcConstraint(MachOLinkingContext::objc_retainReleaseForSimulator);
|
|
|
|
else
|
|
|
|
file.setObjcConstraint(MachOLinkingContext::objc_retainRelease);
|
|
|
|
|
2016-01-16 08:07:22 +08:00
|
|
|
file.setSwiftVersion((flags >> 8) & 0xFF);
|
|
|
|
|
2016-11-11 12:29:25 +08:00
|
|
|
return llvm::Error::success();
|
2016-01-15 07:35:05 +08:00
|
|
|
}
|
|
|
|
|
2014-06-28 02:25:01 +08:00
|
|
|
/// Converts normalized mach-o file into an lld::File and lld::Atoms.
|
2016-03-31 07:43:27 +08:00
|
|
|
llvm::Expected<std::unique_ptr<lld::File>>
|
Separate file parsing from File's constructors.
This is a second patch for InputGraph cleanup.
Sorry about the size of the patch, but what I did in this
patch is basically moving code from constructor to a new
method, parse(), so the amount of new code is small.
This has no change in functionality.
We've discussed the issue that we have too many classes
to represent a concept of "file". We have File subclasses
that represent files read from disk. In addition to that,
we have bunch of InputElement subclasses (that are part
of InputGraph) that represent command line arguments for
input file names. InputElement is a wrapper for File.
InputElement has parseFile method. The method instantiates
a File. The File's constructor reads a file from disk and
parses that.
Because parseFile method is called from multiple worker
threads, file parsing is processed in parallel. In other
words, one reason why we needed the wrapper classes is
because a File would start reading a file as soon as it
is instantiated.
So, the reason why we have too many classes here is at
least partly because of the design flaw of File class.
Just like threads in a good threading library, we need
to separate instantiation from "start" method, so that
we can instantiate File objects when we need them (which
should be very fast because it involves only one mmap()
and no real file IO) and use them directly instead of
the wrapper classes. Later, we call parse() on each
file in parallel to let them do actual file IO.
In this design, we can eliminate a reason to have the
wrapper classes.
In order to minimize the size of the patch, I didn't go so
far as to replace the wrapper classes with File classes.
The wrapper classes are still there.
In this patch, we call parse() immediately after
instantiating a File, so this really has no change in
functionality. Eventually the call of parse() should be
moved to Driver::link(). That'll be done in another patch.
llvm-svn: 224102
2014-12-12 15:31:09 +08:00
|
|
|
objectToAtoms(const NormalizedFile &normalizedFile, StringRef path,
|
|
|
|
bool copyRefs) {
|
2020-06-04 02:13:05 +08:00
|
|
|
auto file = std::make_unique<MachOFile>(path);
|
2016-03-31 07:43:27 +08:00
|
|
|
if (auto ec = normalizedObjectToAtoms(file.get(), normalizedFile, copyRefs))
|
|
|
|
return std::move(ec);
|
Separate file parsing from File's constructors.
This is a second patch for InputGraph cleanup.
Sorry about the size of the patch, but what I did in this
patch is basically moving code from constructor to a new
method, parse(), so the amount of new code is small.
This has no change in functionality.
We've discussed the issue that we have too many classes
to represent a concept of "file". We have File subclasses
that represent files read from disk. In addition to that,
we have bunch of InputElement subclasses (that are part
of InputGraph) that represent command line arguments for
input file names. InputElement is a wrapper for File.
InputElement has parseFile method. The method instantiates
a File. The File's constructor reads a file from disk and
parses that.
Because parseFile method is called from multiple worker
threads, file parsing is processed in parallel. In other
words, one reason why we needed the wrapper classes is
because a File would start reading a file as soon as it
is instantiated.
So, the reason why we have too many classes here is at
least partly because of the design flaw of File class.
Just like threads in a good threading library, we need
to separate instantiation from "start" method, so that
we can instantiate File objects when we need them (which
should be very fast because it involves only one mmap()
and no real file IO) and use them directly instead of
the wrapper classes. Later, we call parse() on each
file in parallel to let them do actual file IO.
In this design, we can eliminate a reason to have the
wrapper classes.
In order to minimize the size of the patch, I didn't go so
far as to replace the wrapper classes with File classes.
The wrapper classes are still there.
In this patch, we call parse() immediately after
instantiating a File, so this really has no change in
functionality. Eventually the call of parse() should be
moved to Driver::link(). That'll be done in another patch.
llvm-svn: 224102
2014-12-12 15:31:09 +08:00
|
|
|
return std::unique_ptr<File>(std::move(file));
|
|
|
|
}
|
|
|
|
|
2016-03-31 07:43:27 +08:00
|
|
|
llvm::Expected<std::unique_ptr<lld::File>>
|
Separate file parsing from File's constructors.
This is a second patch for InputGraph cleanup.
Sorry about the size of the patch, but what I did in this
patch is basically moving code from constructor to a new
method, parse(), so the amount of new code is small.
This has no change in functionality.
We've discussed the issue that we have too many classes
to represent a concept of "file". We have File subclasses
that represent files read from disk. In addition to that,
we have bunch of InputElement subclasses (that are part
of InputGraph) that represent command line arguments for
input file names. InputElement is a wrapper for File.
InputElement has parseFile method. The method instantiates
a File. The File's constructor reads a file from disk and
parses that.
Because parseFile method is called from multiple worker
threads, file parsing is processed in parallel. In other
words, one reason why we needed the wrapper classes is
because a File would start reading a file as soon as it
is instantiated.
So, the reason why we have too many classes here is at
least partly because of the design flaw of File class.
Just like threads in a good threading library, we need
to separate instantiation from "start" method, so that
we can instantiate File objects when we need them (which
should be very fast because it involves only one mmap()
and no real file IO) and use them directly instead of
the wrapper classes. Later, we call parse() on each
file in parallel to let them do actual file IO.
In this design, we can eliminate a reason to have the
wrapper classes.
In order to minimize the size of the patch, I didn't go so
far as to replace the wrapper classes with File classes.
The wrapper classes are still there.
In this patch, we call parse() immediately after
instantiating a File, so this really has no change in
functionality. Eventually the call of parse() should be
moved to Driver::link(). That'll be done in another patch.
llvm-svn: 224102
2014-12-12 15:31:09 +08:00
|
|
|
dylibToAtoms(const NormalizedFile &normalizedFile, StringRef path,
|
|
|
|
bool copyRefs) {
|
|
|
|
// Instantiate SharedLibraryFile object.
|
2020-06-04 02:13:05 +08:00
|
|
|
auto file = std::make_unique<MachODylibFile>(path);
|
2016-03-31 07:43:27 +08:00
|
|
|
if (auto ec = normalizedDylibToAtoms(file.get(), normalizedFile, copyRefs))
|
|
|
|
return std::move(ec);
|
Separate file parsing from File's constructors.
This is a second patch for InputGraph cleanup.
Sorry about the size of the patch, but what I did in this
patch is basically moving code from constructor to a new
method, parse(), so the amount of new code is small.
This has no change in functionality.
We've discussed the issue that we have too many classes
to represent a concept of "file". We have File subclasses
that represent files read from disk. In addition to that,
we have bunch of InputElement subclasses (that are part
of InputGraph) that represent command line arguments for
input file names. InputElement is a wrapper for File.
InputElement has parseFile method. The method instantiates
a File. The File's constructor reads a file from disk and
parses that.
Because parseFile method is called from multiple worker
threads, file parsing is processed in parallel. In other
words, one reason why we needed the wrapper classes is
because a File would start reading a file as soon as it
is instantiated.
So, the reason why we have too many classes here is at
least partly because of the design flaw of File class.
Just like threads in a good threading library, we need
to separate instantiation from "start" method, so that
we can instantiate File objects when we need them (which
should be very fast because it involves only one mmap()
and no real file IO) and use them directly instead of
the wrapper classes. Later, we call parse() on each
file in parallel to let them do actual file IO.
In this design, we can eliminate a reason to have the
wrapper classes.
In order to minimize the size of the patch, I didn't go so
far as to replace the wrapper classes with File classes.
The wrapper classes are still there.
In this patch, we call parse() immediately after
instantiating a File, so this really has no change in
functionality. Eventually the call of parse() should be
moved to Driver::link(). That'll be done in another patch.
llvm-svn: 224102
2014-12-12 15:31:09 +08:00
|
|
|
return std::unique_ptr<File>(std::move(file));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
namespace normalized {
|
|
|
|
|
2016-01-20 05:54:21 +08:00
|
|
|
static bool isObjCImageInfo(const Section §) {
|
|
|
|
return (sect.segmentName == "__OBJC" && sect.sectionName == "__image_info") ||
|
|
|
|
(sect.segmentName == "__DATA" && sect.sectionName == "__objc_imageinfo");
|
|
|
|
}
|
|
|
|
|
2016-03-31 07:43:27 +08:00
|
|
|
llvm::Error
|
Separate file parsing from File's constructors.
This is a second patch for InputGraph cleanup.
Sorry about the size of the patch, but what I did in this
patch is basically moving code from constructor to a new
method, parse(), so the amount of new code is small.
This has no change in functionality.
We've discussed the issue that we have too many classes
to represent a concept of "file". We have File subclasses
that represent files read from disk. In addition to that,
we have bunch of InputElement subclasses (that are part
of InputGraph) that represent command line arguments for
input file names. InputElement is a wrapper for File.
InputElement has parseFile method. The method instantiates
a File. The File's constructor reads a file from disk and
parses that.
Because parseFile method is called from multiple worker
threads, file parsing is processed in parallel. In other
words, one reason why we needed the wrapper classes is
because a File would start reading a file as soon as it
is instantiated.
So, the reason why we have too many classes here is at
least partly because of the design flaw of File class.
Just like threads in a good threading library, we need
to separate instantiation from "start" method, so that
we can instantiate File objects when we need them (which
should be very fast because it involves only one mmap()
and no real file IO) and use them directly instead of
the wrapper classes. Later, we call parse() on each
file in parallel to let them do actual file IO.
In this design, we can eliminate a reason to have the
wrapper classes.
In order to minimize the size of the patch, I didn't go so
far as to replace the wrapper classes with File classes.
The wrapper classes are still there.
In this patch, we call parse() immediately after
instantiating a File, so this really has no change in
functionality. Eventually the call of parse() should be
moved to Driver::link(). That'll be done in another patch.
llvm-svn: 224102
2014-12-12 15:31:09 +08:00
|
|
|
normalizedObjectToAtoms(MachOFile *file,
|
|
|
|
const NormalizedFile &normalizedFile,
|
|
|
|
bool copyRefs) {
|
2018-05-15 21:36:20 +08:00
|
|
|
LLVM_DEBUG(llvm::dbgs() << "******** Normalizing file to atoms: "
|
|
|
|
<< file->path() << "\n");
|
2014-10-22 07:45:37 +08:00
|
|
|
bool scatterable = ((normalizedFile.flags & MH_SUBSECTIONS_VIA_SYMBOLS) != 0);
|
|
|
|
|
2014-06-04 07:36:50 +08:00
|
|
|
// Create atoms from each section.
|
2014-05-30 07:07:20 +08:00
|
|
|
for (auto § : normalizedFile.sections) {
|
2016-07-28 06:55:30 +08:00
|
|
|
|
|
|
|
// If this is a debug-info section parse it specially.
|
2014-06-28 02:25:01 +08:00
|
|
|
if (isDebugInfoSection(sect))
|
|
|
|
continue;
|
2016-01-20 05:54:21 +08:00
|
|
|
|
|
|
|
// If the file contains an objc_image_info struct, then we should parse the
|
|
|
|
// ObjC flags and Swift version.
|
|
|
|
if (isObjCImageInfo(sect)) {
|
2016-03-31 07:43:27 +08:00
|
|
|
if (auto ec = parseObjCImageInfo(sect, normalizedFile, *file))
|
2016-01-20 05:54:21 +08:00
|
|
|
return ec;
|
|
|
|
// We then skip adding atoms for this section as we use the ObjCPass to
|
|
|
|
// re-emit this data after it has been aggregated for all files.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-10-03 01:27:20 +08:00
|
|
|
bool customSectionName;
|
|
|
|
DefinedAtom::ContentType atomType = atomTypeFromSection(sect,
|
|
|
|
customSectionName);
|
2016-03-31 07:43:27 +08:00
|
|
|
if (auto ec = processSection(atomType, sect, customSectionName,
|
|
|
|
normalizedFile, *file, scatterable, copyRefs))
|
2014-05-30 07:07:20 +08:00
|
|
|
return ec;
|
2014-01-14 06:28:02 +08:00
|
|
|
}
|
2014-05-30 07:07:20 +08:00
|
|
|
// Create atoms from undefined symbols.
|
2014-02-03 03:34:55 +08:00
|
|
|
for (auto &sym : normalizedFile.undefinedSymbols) {
|
2020-01-07 02:21:05 +08:00
|
|
|
// Undefined symbols with n_value != 0 are actually tentative definitions.
|
2014-06-04 07:36:50 +08:00
|
|
|
if (sym.value == Hex64(0)) {
|
|
|
|
file->addUndefinedAtom(sym.name, copyRefs);
|
|
|
|
} else {
|
|
|
|
file->addTentativeDefAtom(sym.name, atomScope(sym.scope), sym.value,
|
2015-03-26 10:03:44 +08:00
|
|
|
DefinedAtom::Alignment(1 << (sym.desc >> 8)),
|
|
|
|
copyRefs);
|
2014-06-04 07:36:50 +08:00
|
|
|
}
|
2014-02-03 03:34:55 +08:00
|
|
|
}
|
2014-01-04 07:12:02 +08:00
|
|
|
|
2014-06-28 02:25:01 +08:00
|
|
|
// Convert mach-o relocations to References
|
2014-07-17 03:49:02 +08:00
|
|
|
std::unique_ptr<mach_o::ArchHandler> handler
|
|
|
|
= ArchHandler::create(normalizedFile.arch);
|
2014-06-28 02:25:01 +08:00
|
|
|
for (auto § : normalizedFile.sections) {
|
|
|
|
if (isDebugInfoSection(sect))
|
|
|
|
continue;
|
2016-03-31 04:15:06 +08:00
|
|
|
if (llvm::Error ec = convertRelocs(sect, normalizedFile, scatterable,
|
|
|
|
*file, *handler))
|
2016-03-31 08:35:50 +08:00
|
|
|
return ec;
|
2014-06-28 02:25:01 +08:00
|
|
|
}
|
|
|
|
|
2014-07-23 08:51:37 +08:00
|
|
|
// Add additional arch-specific References
|
|
|
|
file->eachDefinedAtom([&](MachODefinedAtom* atom) -> void {
|
|
|
|
handler->addAdditionalReferences(*atom);
|
|
|
|
});
|
|
|
|
|
2014-10-16 02:19:31 +08:00
|
|
|
// Each __eh_frame section needs references to both __text (the function we're
|
|
|
|
// providing unwind info for) and itself (FDE -> CIE). These aren't
|
|
|
|
// represented in the relocations on some architectures, so we have to add
|
|
|
|
// them back in manually there.
|
2016-03-31 07:43:27 +08:00
|
|
|
if (auto ec = addEHFrameReferences(normalizedFile, *file, *handler))
|
2014-10-16 02:19:31 +08:00
|
|
|
return ec;
|
|
|
|
|
2014-07-25 07:06:56 +08:00
|
|
|
// Process mach-o data-in-code regions array. That information is encoded in
|
|
|
|
// atoms as References at each transition point.
|
|
|
|
unsigned nextIndex = 0;
|
|
|
|
for (const DataInCode &entry : normalizedFile.dataInCode) {
|
|
|
|
++nextIndex;
|
|
|
|
const Section* s = findSectionCoveringAddress(normalizedFile, entry.offset);
|
|
|
|
if (!s) {
|
2016-03-31 07:43:27 +08:00
|
|
|
return llvm::make_error<GenericError>(Twine("LC_DATA_IN_CODE address ("
|
|
|
|
+ Twine(entry.offset)
|
|
|
|
+ ") is not in any section"));
|
2014-07-25 07:06:56 +08:00
|
|
|
}
|
|
|
|
uint64_t offsetInSect = entry.offset - s->address;
|
|
|
|
uint32_t offsetInAtom;
|
|
|
|
MachODefinedAtom *atom = file->findAtomCoveringAddress(*s, offsetInSect,
|
|
|
|
&offsetInAtom);
|
|
|
|
if (offsetInAtom + entry.length > atom->size()) {
|
2016-03-31 07:43:27 +08:00
|
|
|
return llvm::make_error<GenericError>(Twine("LC_DATA_IN_CODE entry "
|
|
|
|
"(offset="
|
|
|
|
+ Twine(entry.offset)
|
|
|
|
+ ", length="
|
|
|
|
+ Twine(entry.length)
|
|
|
|
+ ") crosses atom boundary."));
|
2014-07-25 07:06:56 +08:00
|
|
|
}
|
|
|
|
// Add reference that marks start of data-in-code.
|
2016-02-02 07:42:10 +08:00
|
|
|
atom->addReference(Reference::KindNamespace::mach_o, handler->kindArch(),
|
|
|
|
handler->dataInCodeTransitionStart(*atom),
|
|
|
|
offsetInAtom, atom, entry.kind);
|
2014-07-25 07:06:56 +08:00
|
|
|
|
|
|
|
// Peek at next entry, if it starts where this one ends, skip ending ref.
|
|
|
|
if (nextIndex < normalizedFile.dataInCode.size()) {
|
|
|
|
const DataInCode &nextEntry = normalizedFile.dataInCode[nextIndex];
|
|
|
|
if (nextEntry.offset == (entry.offset + entry.length))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If data goes to end of function, skip ending ref.
|
|
|
|
if ((offsetInAtom + entry.length) == atom->size())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Add reference that marks end of data-in-code.
|
2016-02-02 07:42:10 +08:00
|
|
|
atom->addReference(Reference::KindNamespace::mach_o, handler->kindArch(),
|
|
|
|
handler->dataInCodeTransitionEnd(*atom),
|
|
|
|
offsetInAtom+entry.length, atom, 0);
|
2014-07-25 07:06:56 +08:00
|
|
|
}
|
|
|
|
|
2016-01-15 07:25:06 +08:00
|
|
|
// Cache some attributes on the file for use later.
|
2016-01-16 09:09:23 +08:00
|
|
|
file->setFlags(normalizedFile.flags);
|
2016-01-15 07:25:06 +08:00
|
|
|
file->setArch(normalizedFile.arch);
|
|
|
|
file->setOS(normalizedFile.os);
|
2016-02-04 10:16:08 +08:00
|
|
|
file->setMinVersion(normalizedFile.minOSverson);
|
|
|
|
file->setMinVersionLoadCommandKind(normalizedFile.minOSVersionKind);
|
2016-01-15 07:25:06 +08:00
|
|
|
|
2014-06-28 02:25:01 +08:00
|
|
|
// Sort references in each atom to their canonical order.
|
|
|
|
for (const DefinedAtom* defAtom : file->defined()) {
|
|
|
|
reinterpret_cast<const SimpleDefinedAtom*>(defAtom)->sortReferences();
|
|
|
|
}
|
2016-07-28 06:55:30 +08:00
|
|
|
|
|
|
|
if (auto err = parseDebugInfo(*file, normalizedFile, copyRefs))
|
|
|
|
return err;
|
|
|
|
|
2016-11-11 12:29:25 +08:00
|
|
|
return llvm::Error::success();
|
2014-01-04 07:12:02 +08:00
|
|
|
}
|
|
|
|
|
2016-03-31 07:43:27 +08:00
|
|
|
llvm::Error
|
Separate file parsing from File's constructors.
This is a second patch for InputGraph cleanup.
Sorry about the size of the patch, but what I did in this
patch is basically moving code from constructor to a new
method, parse(), so the amount of new code is small.
This has no change in functionality.
We've discussed the issue that we have too many classes
to represent a concept of "file". We have File subclasses
that represent files read from disk. In addition to that,
we have bunch of InputElement subclasses (that are part
of InputGraph) that represent command line arguments for
input file names. InputElement is a wrapper for File.
InputElement has parseFile method. The method instantiates
a File. The File's constructor reads a file from disk and
parses that.
Because parseFile method is called from multiple worker
threads, file parsing is processed in parallel. In other
words, one reason why we needed the wrapper classes is
because a File would start reading a file as soon as it
is instantiated.
So, the reason why we have too many classes here is at
least partly because of the design flaw of File class.
Just like threads in a good threading library, we need
to separate instantiation from "start" method, so that
we can instantiate File objects when we need them (which
should be very fast because it involves only one mmap()
and no real file IO) and use them directly instead of
the wrapper classes. Later, we call parse() on each
file in parallel to let them do actual file IO.
In this design, we can eliminate a reason to have the
wrapper classes.
In order to minimize the size of the patch, I didn't go so
far as to replace the wrapper classes with File classes.
The wrapper classes are still there.
In this patch, we call parse() immediately after
instantiating a File, so this really has no change in
functionality. Eventually the call of parse() should be
moved to Driver::link(). That'll be done in another patch.
llvm-svn: 224102
2014-12-12 15:31:09 +08:00
|
|
|
normalizedDylibToAtoms(MachODylibFile *file,
|
|
|
|
const NormalizedFile &normalizedFile,
|
2014-06-30 17:11:38 +08:00
|
|
|
bool copyRefs) {
|
Separate file parsing from File's constructors.
This is a second patch for InputGraph cleanup.
Sorry about the size of the patch, but what I did in this
patch is basically moving code from constructor to a new
method, parse(), so the amount of new code is small.
This has no change in functionality.
We've discussed the issue that we have too many classes
to represent a concept of "file". We have File subclasses
that represent files read from disk. In addition to that,
we have bunch of InputElement subclasses (that are part
of InputGraph) that represent command line arguments for
input file names. InputElement is a wrapper for File.
InputElement has parseFile method. The method instantiates
a File. The File's constructor reads a file from disk and
parses that.
Because parseFile method is called from multiple worker
threads, file parsing is processed in parallel. In other
words, one reason why we needed the wrapper classes is
because a File would start reading a file as soon as it
is instantiated.
So, the reason why we have too many classes here is at
least partly because of the design flaw of File class.
Just like threads in a good threading library, we need
to separate instantiation from "start" method, so that
we can instantiate File objects when we need them (which
should be very fast because it involves only one mmap()
and no real file IO) and use them directly instead of
the wrapper classes. Later, we call parse() on each
file in parallel to let them do actual file IO.
In this design, we can eliminate a reason to have the
wrapper classes.
In order to minimize the size of the patch, I didn't go so
far as to replace the wrapper classes with File classes.
The wrapper classes are still there.
In this patch, we call parse() immediately after
instantiating a File, so this really has no change in
functionality. Eventually the call of parse() should be
moved to Driver::link(). That'll be done in another patch.
llvm-svn: 224102
2014-12-12 15:31:09 +08:00
|
|
|
file->setInstallName(normalizedFile.installName);
|
|
|
|
file->setCompatVersion(normalizedFile.compatVersion);
|
|
|
|
file->setCurrentVersion(normalizedFile.currentVersion);
|
|
|
|
|
2014-08-14 07:55:41 +08:00
|
|
|
// Tell MachODylibFile object about all symbols it exports.
|
2014-09-04 03:52:50 +08:00
|
|
|
if (!normalizedFile.exportInfo.empty()) {
|
|
|
|
// If exports trie exists, use it instead of traditional symbol table.
|
|
|
|
for (const Export &exp : normalizedFile.exportInfo) {
|
|
|
|
bool weakDef = (exp.flags & EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION);
|
|
|
|
// StringRefs from export iterator are ephemeral, so force copy.
|
|
|
|
file->addExportedSymbol(exp.name, weakDef, true);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (auto &sym : normalizedFile.globalSymbols) {
|
|
|
|
assert((sym.scope & N_EXT) && "only expect external symbols here");
|
|
|
|
bool weakDef = (sym.desc & N_WEAK_DEF);
|
|
|
|
file->addExportedSymbol(sym.name, weakDef, copyRefs);
|
|
|
|
}
|
2014-08-14 07:55:41 +08:00
|
|
|
}
|
|
|
|
// Tell MachODylibFile object about all dylibs it re-exports.
|
|
|
|
for (const DependentDylib &dep : normalizedFile.dependentDylibs) {
|
|
|
|
if (dep.kind == llvm::MachO::LC_REEXPORT_DYLIB)
|
|
|
|
file->addReExportedDylib(dep.path);
|
2014-06-30 17:11:38 +08:00
|
|
|
}
|
2016-11-11 12:29:25 +08:00
|
|
|
return llvm::Error::success();
|
2014-06-30 17:11:38 +08:00
|
|
|
}
|
|
|
|
|
2014-06-10 09:50:00 +08:00
|
|
|
void relocatableSectionInfoForContentType(DefinedAtom::ContentType atomType,
|
|
|
|
StringRef &segmentName,
|
|
|
|
StringRef §ionName,
|
|
|
|
SectionType §ionType,
|
2016-01-08 05:07:26 +08:00
|
|
|
SectionAttr §ionAttrs,
|
|
|
|
bool &relocsToDefinedCanBeImplicit) {
|
2014-06-10 09:50:00 +08:00
|
|
|
|
|
|
|
for (const MachORelocatableSectionToAtomType *p = sectsToAtomType ;
|
|
|
|
p->atomType != DefinedAtom::typeUnknown; ++p) {
|
|
|
|
if (p->atomType != atomType)
|
|
|
|
continue;
|
|
|
|
// Wild carded entries are ignored for reverse lookups.
|
|
|
|
if (p->segmentName.empty() || p->sectionName.empty())
|
|
|
|
continue;
|
|
|
|
segmentName = p->segmentName;
|
|
|
|
sectionName = p->sectionName;
|
|
|
|
sectionType = p->sectionType;
|
|
|
|
sectionAttrs = 0;
|
2016-01-08 05:07:26 +08:00
|
|
|
relocsToDefinedCanBeImplicit = false;
|
2014-06-10 09:50:00 +08:00
|
|
|
if (atomType == DefinedAtom::typeCode)
|
|
|
|
sectionAttrs = S_ATTR_PURE_INSTRUCTIONS;
|
2016-01-08 05:07:26 +08:00
|
|
|
if (atomType == DefinedAtom::typeCFI)
|
|
|
|
relocsToDefinedCanBeImplicit = true;
|
2014-06-10 09:50:00 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
llvm_unreachable("content type not yet supported");
|
|
|
|
}
|
|
|
|
|
2016-03-31 07:43:27 +08:00
|
|
|
llvm::Expected<std::unique_ptr<lld::File>>
|
2014-01-11 09:07:43 +08:00
|
|
|
normalizedToAtoms(const NormalizedFile &normalizedFile, StringRef path,
|
|
|
|
bool copyRefs) {
|
2014-01-04 07:12:02 +08:00
|
|
|
switch (normalizedFile.fileType) {
|
2014-06-30 17:11:38 +08:00
|
|
|
case MH_DYLIB:
|
2014-08-14 07:55:41 +08:00
|
|
|
case MH_DYLIB_STUB:
|
Separate file parsing from File's constructors.
This is a second patch for InputGraph cleanup.
Sorry about the size of the patch, but what I did in this
patch is basically moving code from constructor to a new
method, parse(), so the amount of new code is small.
This has no change in functionality.
We've discussed the issue that we have too many classes
to represent a concept of "file". We have File subclasses
that represent files read from disk. In addition to that,
we have bunch of InputElement subclasses (that are part
of InputGraph) that represent command line arguments for
input file names. InputElement is a wrapper for File.
InputElement has parseFile method. The method instantiates
a File. The File's constructor reads a file from disk and
parses that.
Because parseFile method is called from multiple worker
threads, file parsing is processed in parallel. In other
words, one reason why we needed the wrapper classes is
because a File would start reading a file as soon as it
is instantiated.
So, the reason why we have too many classes here is at
least partly because of the design flaw of File class.
Just like threads in a good threading library, we need
to separate instantiation from "start" method, so that
we can instantiate File objects when we need them (which
should be very fast because it involves only one mmap()
and no real file IO) and use them directly instead of
the wrapper classes. Later, we call parse() on each
file in parallel to let them do actual file IO.
In this design, we can eliminate a reason to have the
wrapper classes.
In order to minimize the size of the patch, I didn't go so
far as to replace the wrapper classes with File classes.
The wrapper classes are still there.
In this patch, we call parse() immediately after
instantiating a File, so this really has no change in
functionality. Eventually the call of parse() should be
moved to Driver::link(). That'll be done in another patch.
llvm-svn: 224102
2014-12-12 15:31:09 +08:00
|
|
|
return dylibToAtoms(normalizedFile, path, copyRefs);
|
2014-01-04 07:12:02 +08:00
|
|
|
case MH_OBJECT:
|
Separate file parsing from File's constructors.
This is a second patch for InputGraph cleanup.
Sorry about the size of the patch, but what I did in this
patch is basically moving code from constructor to a new
method, parse(), so the amount of new code is small.
This has no change in functionality.
We've discussed the issue that we have too many classes
to represent a concept of "file". We have File subclasses
that represent files read from disk. In addition to that,
we have bunch of InputElement subclasses (that are part
of InputGraph) that represent command line arguments for
input file names. InputElement is a wrapper for File.
InputElement has parseFile method. The method instantiates
a File. The File's constructor reads a file from disk and
parses that.
Because parseFile method is called from multiple worker
threads, file parsing is processed in parallel. In other
words, one reason why we needed the wrapper classes is
because a File would start reading a file as soon as it
is instantiated.
So, the reason why we have too many classes here is at
least partly because of the design flaw of File class.
Just like threads in a good threading library, we need
to separate instantiation from "start" method, so that
we can instantiate File objects when we need them (which
should be very fast because it involves only one mmap()
and no real file IO) and use them directly instead of
the wrapper classes. Later, we call parse() on each
file in parallel to let them do actual file IO.
In this design, we can eliminate a reason to have the
wrapper classes.
In order to minimize the size of the patch, I didn't go so
far as to replace the wrapper classes with File classes.
The wrapper classes are still there.
In this patch, we call parse() immediately after
instantiating a File, so this really has no change in
functionality. Eventually the call of parse() should be
moved to Driver::link(). That'll be done in another patch.
llvm-svn: 224102
2014-12-12 15:31:09 +08:00
|
|
|
return objectToAtoms(normalizedFile, path, copyRefs);
|
2014-01-04 07:12:02 +08:00
|
|
|
default:
|
|
|
|
llvm_unreachable("unhandled MachO file type!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace normalized
|
|
|
|
} // namespace mach_o
|
|
|
|
} // namespace lld
|