[MachO] Add undefined atoms.

llvm-svn: 200649
This commit is contained in:
Joey Gouly 2014-02-02 19:34:55 +00:00
parent cc8068f2ac
commit cf466800b7
3 changed files with 27 additions and 6 deletions

View File

@ -37,6 +37,18 @@ public:
addAtom(*atom);
}
void addUndefinedAtom(StringRef name, bool copyRefs) {
if (copyRefs) {
// Make a copy of the atom's name and content that is owned by this file.
char *s = _allocator.Allocate<char>(name.size());
memcpy(s, name.data(), name.size());
name = StringRef(s, name.size());
}
SimpleUndefinedAtom *atom =
new (_allocator) SimpleUndefinedAtom(*this, name);
addAtom(*atom);
}
private:
llvm::BumpPtrAllocator _allocator;
};

View File

@ -98,8 +98,9 @@ normalizedObjectToAtoms(const NormalizedFile &normalizedFile, StringRef path,
processSymbol(normalizedFile, *file, sym, copyRefs);
}
assert(normalizedFile.undefinedSymbols.empty() &&
"undefined symbols not supported yet!");
for (auto &sym : normalizedFile.undefinedSymbols) {
file->addUndefinedAtom(sym.name, copyRefs);
}
return std::unique_ptr<File>(std::move(file));
}

View File

@ -23,8 +23,8 @@ using namespace llvm::MachO;
TEST(ToAtomsTest, empty_obj_x86_64) {
NormalizedFile f;
f.arch = lld::MachOLinkingContext::arch_x86_64;
ErrorOr<std::unique_ptr<const lld::File>> atom_f = normalizedToAtoms(f, "",
false);
ErrorOr<std::unique_ptr<const lld::File>> atom_f =
normalizedToAtoms(f, "", false);
EXPECT_FALSE(!atom_f);
EXPECT_EQ(0U, (*atom_f)->defined().size());
}
@ -51,6 +51,10 @@ TEST(ToAtomsTest, basic_obj_x86_64) {
barSymbol.sect = 1;
barSymbol.value = 2;
f.globalSymbols.push_back(barSymbol);
Symbol undefSym;
undefSym.name = "_undef";
undefSym.type = N_UNDF;
f.undefinedSymbols.push_back(undefSym);
Symbol bazSymbol;
bazSymbol.name = "_baz";
bazSymbol.type = N_SECT;
@ -59,8 +63,8 @@ TEST(ToAtomsTest, basic_obj_x86_64) {
bazSymbol.value = 3;
f.localSymbols.push_back(bazSymbol);
ErrorOr<std::unique_ptr<const lld::File>> atom_f = normalizedToAtoms(f, "",
false);
ErrorOr<std::unique_ptr<const lld::File>> atom_f =
normalizedToAtoms(f, "", false);
EXPECT_FALSE(!atom_f);
const lld::File &file = **atom_f;
EXPECT_EQ(3U, file.defined().size());
@ -70,6 +74,7 @@ TEST(ToAtomsTest, basic_obj_x86_64) {
const lld::DefinedAtom *atom2 = *it;
++it;
const lld::DefinedAtom *atom3 = *it;
const lld::UndefinedAtom *atom4 = *file.undefined().begin();
EXPECT_TRUE(atom1->name().equals("_foo"));
EXPECT_EQ(2U, atom1->rawContent().size());
EXPECT_EQ(0x90, atom1->rawContent()[0]);
@ -85,4 +90,7 @@ TEST(ToAtomsTest, basic_obj_x86_64) {
EXPECT_EQ(1U, atom3->rawContent().size());
EXPECT_EQ(0xC4, atom3->rawContent()[0]);
EXPECT_EQ(lld::Atom::scopeLinkageUnit, atom3->scope());
EXPECT_TRUE(atom4->name().equals("_undef"));
EXPECT_EQ(lld::Atom::definitionUndefined, atom4->definition());
}