2014-01-04 07:12:02 +08:00
|
|
|
//===- lib/ReaderWriter/MachO/File.h --------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLD_READER_WRITER_MACHO_FILE_H
|
|
|
|
#define LLD_READER_WRITER_MACHO_FILE_H
|
|
|
|
|
2014-06-28 02:25:01 +08:00
|
|
|
#include "llvm/ADT/StringMap.h"
|
|
|
|
|
2014-01-04 07:12:02 +08:00
|
|
|
#include "Atoms.h"
|
|
|
|
|
2014-06-12 05:47:51 +08:00
|
|
|
#include "lld/Core/Simple.h"
|
2014-01-04 07:12:02 +08:00
|
|
|
|
|
|
|
namespace lld {
|
|
|
|
namespace mach_o {
|
|
|
|
|
2014-06-28 02:25:01 +08:00
|
|
|
using lld::mach_o::normalized::Section;
|
|
|
|
|
2014-01-04 07:12:02 +08:00
|
|
|
class MachOFile : public SimpleFile {
|
|
|
|
public:
|
|
|
|
MachOFile(StringRef path) : SimpleFile(path) {}
|
|
|
|
|
2014-05-16 07:03:50 +08:00
|
|
|
void addDefinedAtom(StringRef name, Atom::Scope scope,
|
2014-05-28 09:16:35 +08:00
|
|
|
DefinedAtom::ContentType type, DefinedAtom::Merge merge,
|
2014-06-28 02:25:01 +08:00
|
|
|
uint64_t sectionOffset, uint64_t contentSize,
|
|
|
|
bool copyRefs, const Section *inSection) {
|
|
|
|
assert(sectionOffset+contentSize <= inSection->content.size());
|
|
|
|
ArrayRef<uint8_t> content = inSection->content.slice(sectionOffset,
|
|
|
|
contentSize);
|
2014-01-14 06:28:02 +08:00
|
|
|
if (copyRefs) {
|
2014-01-11 09:07:43 +08:00
|
|
|
// Make a copy of the atom's name and content that is owned by this file.
|
2014-02-07 07:48:52 +08:00
|
|
|
name = name.copy(_allocator);
|
|
|
|
content = content.copy(_allocator);
|
2014-01-11 09:07:43 +08:00
|
|
|
}
|
2014-01-04 07:12:02 +08:00
|
|
|
MachODefinedAtom *atom =
|
2014-05-28 09:16:35 +08:00
|
|
|
new (_allocator) MachODefinedAtom(*this, name, scope, type, merge,
|
|
|
|
content);
|
2014-06-28 03:08:56 +08:00
|
|
|
addAtomForSection(inSection, atom, sectionOffset);
|
2014-05-16 07:03:50 +08:00
|
|
|
}
|
|
|
|
|
2014-05-31 06:51:04 +08:00
|
|
|
void addDefinedAtomInCustomSection(StringRef name, Atom::Scope scope,
|
|
|
|
DefinedAtom::ContentType type, DefinedAtom::Merge merge,
|
2014-06-28 02:25:01 +08:00
|
|
|
uint64_t sectionOffset, uint64_t contentSize,
|
|
|
|
StringRef sectionName, bool copyRefs,
|
|
|
|
const Section *inSection) {
|
|
|
|
assert(sectionOffset+contentSize <= inSection->content.size());
|
|
|
|
ArrayRef<uint8_t> content = inSection->content.slice(sectionOffset,
|
|
|
|
contentSize);
|
|
|
|
if (copyRefs) {
|
2014-05-31 06:51:04 +08:00
|
|
|
// Make a copy of the atom's name and content that is owned by this file.
|
|
|
|
name = name.copy(_allocator);
|
|
|
|
content = content.copy(_allocator);
|
|
|
|
sectionName = sectionName.copy(_allocator);
|
|
|
|
}
|
|
|
|
MachODefinedCustomSectionAtom *atom =
|
|
|
|
new (_allocator) MachODefinedCustomSectionAtom(*this, name, scope, type,
|
|
|
|
merge, content, sectionName);
|
2014-06-28 03:08:56 +08:00
|
|
|
addAtomForSection(inSection, atom, sectionOffset);
|
2014-05-31 06:51:04 +08:00
|
|
|
}
|
|
|
|
|
2014-06-28 02:25:01 +08:00
|
|
|
void addZeroFillDefinedAtom(StringRef name, Atom::Scope scope,
|
|
|
|
uint64_t sectionOffset, uint64_t size,
|
|
|
|
bool copyRefs, const Section *inSection) {
|
2014-05-16 07:03:50 +08:00
|
|
|
if (copyRefs) {
|
|
|
|
// Make a copy of the atom's name and content that is owned by this file.
|
|
|
|
name = name.copy(_allocator);
|
|
|
|
}
|
|
|
|
MachODefinedAtom *atom =
|
|
|
|
new (_allocator) MachODefinedAtom(*this, name, scope, size);
|
2014-06-28 03:08:56 +08:00
|
|
|
addAtomForSection(inSection, atom, sectionOffset);
|
2014-01-04 07:12:02 +08:00
|
|
|
}
|
|
|
|
|
2014-02-03 03:34:55 +08:00
|
|
|
void addUndefinedAtom(StringRef name, bool copyRefs) {
|
|
|
|
if (copyRefs) {
|
2014-02-07 07:48:52 +08:00
|
|
|
// Make a copy of the atom's name that is owned by this file.
|
|
|
|
name = name.copy(_allocator);
|
2014-02-03 03:34:55 +08:00
|
|
|
}
|
|
|
|
SimpleUndefinedAtom *atom =
|
|
|
|
new (_allocator) SimpleUndefinedAtom(*this, name);
|
|
|
|
addAtom(*atom);
|
2014-06-28 02:25:01 +08:00
|
|
|
_undefAtoms[name] = atom;
|
2014-02-03 03:34:55 +08:00
|
|
|
}
|
|
|
|
|
2014-05-16 04:59:23 +08:00
|
|
|
void addTentativeDefAtom(StringRef name, Atom::Scope scope, uint64_t size,
|
|
|
|
DefinedAtom::Alignment align, bool copyRefs) {
|
|
|
|
if (copyRefs) {
|
|
|
|
// Make a copy of the atom's name that is owned by this file.
|
|
|
|
name = name.copy(_allocator);
|
|
|
|
}
|
|
|
|
MachOTentativeDefAtom *atom =
|
|
|
|
new (_allocator) MachOTentativeDefAtom(*this, name, scope, size, align);
|
|
|
|
addAtom(*atom);
|
2014-06-28 02:25:01 +08:00
|
|
|
_undefAtoms[name] = atom;
|
2014-05-16 04:59:23 +08:00
|
|
|
}
|
2014-06-28 02:25:01 +08:00
|
|
|
|
|
|
|
/// Search this file for an the atom from 'section' that covers
|
|
|
|
/// 'offsetInSect'. Returns nullptr is no atom found.
|
|
|
|
MachODefinedAtom *findAtomCoveringAddress(const Section §ion,
|
|
|
|
uint64_t offsetInSect,
|
|
|
|
uint32_t *foundOffsetAtom=nullptr) {
|
|
|
|
auto pos = _sectionAtoms.find(§ion);
|
|
|
|
if (pos == _sectionAtoms.end())
|
|
|
|
return nullptr;
|
|
|
|
auto vec = pos->second;
|
|
|
|
assert(offsetInSect < section.content.size());
|
|
|
|
// Vector of atoms for section are already sorted, so do binary search.
|
|
|
|
auto atomPos = std::lower_bound(vec.begin(), vec.end(), offsetInSect,
|
|
|
|
[offsetInSect](const SectionOffsetAndAtom &ao,
|
|
|
|
uint64_t targetAddr) -> bool {
|
|
|
|
// Each atom has a start offset of its slice of the
|
|
|
|
// section's content. This compare function must return true
|
|
|
|
// iff the atom's range is before the offset being searched for.
|
|
|
|
uint64_t atomsEndOffset = ao.offset+ao.atom->rawContent().size();
|
|
|
|
return (atomsEndOffset <= offsetInSect);
|
|
|
|
});
|
|
|
|
if (atomPos == vec.end())
|
|
|
|
return nullptr;
|
|
|
|
if (foundOffsetAtom)
|
|
|
|
*foundOffsetAtom = offsetInSect - atomPos->offset;
|
|
|
|
return atomPos->atom;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Searches this file for an UndefinedAtom named 'name'. Returns
|
|
|
|
/// nullptr is no such atom found.
|
|
|
|
const lld::Atom *findUndefAtom(StringRef name) {
|
|
|
|
auto pos = _undefAtoms.find(name);
|
|
|
|
if (pos == _undefAtoms.end())
|
|
|
|
return nullptr;
|
|
|
|
return pos->second;
|
|
|
|
}
|
|
|
|
|
2014-01-04 07:12:02 +08:00
|
|
|
private:
|
2014-06-28 02:25:01 +08:00
|
|
|
struct SectionOffsetAndAtom { uint64_t offset; MachODefinedAtom *atom; };
|
2014-06-28 03:08:56 +08:00
|
|
|
|
|
|
|
void addAtomForSection(const Section *inSection, MachODefinedAtom* atom,
|
|
|
|
uint64_t sectionOffset) {
|
|
|
|
SectionOffsetAndAtom offAndAtom;
|
|
|
|
offAndAtom.offset = sectionOffset;
|
|
|
|
offAndAtom.atom = atom;
|
|
|
|
_sectionAtoms[inSection].push_back(offAndAtom);
|
|
|
|
addAtom(*atom);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-28 02:25:01 +08:00
|
|
|
typedef llvm::DenseMap<const normalized::Section *,
|
|
|
|
std::vector<SectionOffsetAndAtom>> SectionToAtoms;
|
|
|
|
typedef llvm::StringMap<const lld::Atom *> NameToAtom;
|
|
|
|
|
|
|
|
llvm::BumpPtrAllocator _allocator;
|
|
|
|
SectionToAtoms _sectionAtoms;
|
|
|
|
NameToAtom _undefAtoms;
|
2014-01-04 07:12:02 +08:00
|
|
|
};
|
|
|
|
|
2014-06-28 02:25:01 +08:00
|
|
|
|
|
|
|
|
2014-01-04 07:12:02 +08:00
|
|
|
} // end namespace mach_o
|
|
|
|
} // end namespace lld
|
|
|
|
|
|
|
|
#endif
|