[lld] r326835 - Implement --just-symbols.
Rui Ueyama via llvm-commits
llvm-commits at lists.llvm.org
Tue Mar 6 13:25:37 PST 2018
Author: ruiu
Date: Tue Mar 6 13:25:37 2018
New Revision: 326835
URL: http://llvm.org/viewvc/llvm-project?rev=326835&view=rev
Log:
Implement --just-symbols.
Differential Revision: https://reviews.llvm.org/D39348
Added:
lld/trunk/test/ELF/Inputs/just-symbols.s
lld/trunk/test/ELF/just-symbols.s
Modified:
lld/trunk/ELF/Driver.cpp
lld/trunk/ELF/InputFiles.cpp
lld/trunk/ELF/InputFiles.h
lld/trunk/ELF/Options.td
Modified: lld/trunk/ELF/Driver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Driver.cpp?rev=326835&r1=326834&r2=326835&view=diff
==============================================================================
--- lld/trunk/ELF/Driver.cpp (original)
+++ lld/trunk/ELF/Driver.cpp Tue Mar 6 13:25:37 2018
@@ -1072,6 +1072,12 @@ template <class ELFT> void LinkerDriver:
for (StringRef S : Config->Undefined)
Symtab->fetchIfLazy<ELFT>(S);
+ // Handle the --just-symbols option. This may add absolute symbols
+ // to the symbol table.
+ for (auto *Arg : Args.filtered(OPT_just_symbols))
+ if (Optional<MemoryBufferRef> MB = readFile(Arg->getValue()))
+ readJustSymbolsFile<ELFT>(*MB);
+
// If an entry symbol is in a static archive, pull out that file now
// to complete the symbol table. After this, no new names except a
// few linker-synthesized ones will be added to the symbol table.
Modified: lld/trunk/ELF/InputFiles.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputFiles.cpp?rev=326835&r1=326834&r2=326835&view=diff
==============================================================================
--- lld/trunk/ELF/InputFiles.cpp (original)
+++ lld/trunk/ELF/InputFiles.cpp Tue Mar 6 13:25:37 2018
@@ -1175,6 +1175,42 @@ std::vector<StringRef> LazyObjFile::getS
}
}
+// This is for --just-symbols.
+//
+// This option allows you to link your output against other existing
+// program, so that if you load both your program and the other program
+// into memory, your output can use program's symbols.
+//
+// What we are doing here is to read defined symbols from a given ELF
+// file and add them as absolute symbols.
+template <class ELFT> void elf::readJustSymbolsFile(MemoryBufferRef MB) {
+ typedef typename ELFT::Shdr Elf_Shdr;
+ typedef typename ELFT::Sym Elf_Sym;
+ typedef typename ELFT::SymRange Elf_Sym_Range;
+
+ StringRef ObjName = MB.getBufferIdentifier();
+ ELFFile<ELFT> Obj = check(ELFFile<ELFT>::create(MB.getBuffer()));
+ ArrayRef<Elf_Shdr> Sections = CHECK(Obj.sections(), ObjName);
+
+ for (const Elf_Shdr &Sec : Sections) {
+ if (Sec.sh_type != SHT_SYMTAB)
+ continue;
+
+ Elf_Sym_Range Syms = CHECK(Obj.symbols(&Sec), ObjName);
+ uint32_t FirstNonLocal = Sec.sh_info;
+ StringRef StringTable =
+ CHECK(Obj.getStringTableForSymtab(Sec, Sections), ObjName);
+
+ std::vector<std::pair<StringRef, uint64_t>> Ret;
+ for (const Elf_Sym &Sym : Syms.slice(FirstNonLocal))
+ if (Sym.st_shndx != SHN_UNDEF)
+ Symtab->addRegular(CHECK(Sym.getName(StringTable), ObjName),
+ Sym.st_other, Sym.getType(), Sym.st_value,
+ Sym.st_size, Sym.getBinding(), nullptr, nullptr);
+ return;
+ }
+}
+
template void ArchiveFile::parse<ELF32LE>();
template void ArchiveFile::parse<ELF32BE>();
template void ArchiveFile::parse<ELF64LE>();
@@ -1204,3 +1240,8 @@ template class elf::SharedFile<ELF32LE>;
template class elf::SharedFile<ELF32BE>;
template class elf::SharedFile<ELF64LE>;
template class elf::SharedFile<ELF64BE>;
+
+template void elf::readJustSymbolsFile<ELF32LE>(MemoryBufferRef);
+template void elf::readJustSymbolsFile<ELF32BE>(MemoryBufferRef);
+template void elf::readJustSymbolsFile<ELF64LE>(MemoryBufferRef);
+template void elf::readJustSymbolsFile<ELF64BE>(MemoryBufferRef);
Modified: lld/trunk/ELF/InputFiles.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputFiles.h?rev=326835&r1=326834&r2=326835&view=diff
==============================================================================
--- lld/trunk/ELF/InputFiles.h (original)
+++ lld/trunk/ELF/InputFiles.h Tue Mar 6 13:25:37 2018
@@ -332,6 +332,9 @@ InputFile *createObjectFile(MemoryBuffer
uint64_t OffsetInArchive = 0);
InputFile *createSharedFile(MemoryBufferRef MB, StringRef DefaultSoName);
+// For --just-symbols
+template <class ELFT> void readJustSymbolsFile(MemoryBufferRef MB);
+
extern std::vector<BinaryFile *> BinaryFiles;
extern std::vector<BitcodeFile *> BitcodeFiles;
extern std::vector<InputFile *> ObjectFiles;
Modified: lld/trunk/ELF/Options.td
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Options.td?rev=326835&r1=326834&r2=326835&view=diff
==============================================================================
--- lld/trunk/ELF/Options.td (original)
+++ lld/trunk/ELF/Options.td Tue Mar 6 13:25:37 2018
@@ -180,6 +180,8 @@ defm image_base : Eq<"image-base">, Help
defm init: Eq<"init">, HelpText<"Specify an initializer function">,
MetaVarName<"<symbol>">;
+defm just_symbols: Eq<"just-symbols">, HelpText<"Just link symbols">;
+
defm library: Eq<"library">, HelpText<"Root name of library to use">,
MetaVarName<"<libName>">;
Added: lld/trunk/test/ELF/Inputs/just-symbols.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/Inputs/just-symbols.s?rev=326835&view=auto
==============================================================================
--- lld/trunk/test/ELF/Inputs/just-symbols.s (added)
+++ lld/trunk/test/ELF/Inputs/just-symbols.s Tue Mar 6 13:25:37 2018
@@ -0,0 +1,9 @@
+.globl foo, bar
+foo:
+ ret
+
+.section .data
+.type bar, @object
+.size bar, 40
+bar:
+ .zero 40
Added: lld/trunk/test/ELF/just-symbols.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/just-symbols.s?rev=326835&view=auto
==============================================================================
--- lld/trunk/test/ELF/just-symbols.s (added)
+++ lld/trunk/test/ELF/just-symbols.s Tue Mar 6 13:25:37 2018
@@ -0,0 +1,17 @@
+# REQUIRES: x86
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %S/Inputs/just-symbols.s -o %t1
+# RUN: ld.lld %t1 -o %t1.exe -Ttext=0x10000
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t2
+# RUN: ld.lld %t2 -just-symbols=%t1.exe -o %t2.exe
+# RUN: llvm-readelf -symbols %t2.exe | FileCheck %s
+
+# CHECK: 0000000000011000 40 OBJECT GLOBAL DEFAULT ABS bar
+# CHECK: 0000000000010000 0 NOTYPE GLOBAL DEFAULT ABS foo
+
+.globl _start
+_start:
+ call foo
+ call bar
+ ret
More information about the llvm-commits
mailing list