From 09fcd4c34c58e0bd9f372e97abd717ba5b4c11d3 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Tue, 6 Mar 2018 21:25:37 +0000 Subject: [PATCH] Implement --just-symbols. Differential Revision: https://reviews.llvm.org/D39348 llvm-svn: 326835 --- lld/ELF/Driver.cpp | 6 ++++++ lld/ELF/InputFiles.cpp | 41 ++++++++++++++++++++++++++++++++++++++ lld/ELF/InputFiles.h | 3 +++ lld/ELF/Options.td | 2 ++ lld/test/ELF/Inputs/just-symbols.s | 9 +++++++++ lld/test/ELF/just-symbols.s | 17 ++++++++++++++++ 6 files changed, 78 insertions(+) create mode 100644 lld/test/ELF/Inputs/just-symbols.s create mode 100644 lld/test/ELF/just-symbols.s diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp index 8abac81..ebfab48 100644 --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -1072,6 +1072,12 @@ template void LinkerDriver::link(opt::InputArgList &Args) { for (StringRef S : Config->Undefined) Symtab->fetchIfLazy(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 MB = readFile(Arg->getValue())) + readJustSymbolsFile(*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. diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index 0bff035..04034ae 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -1175,6 +1175,42 @@ std::vector LazyObjFile::getSymbolNames() { } } +// 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 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 Obj = check(ELFFile::create(MB.getBuffer())); + ArrayRef 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> 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(); template void ArchiveFile::parse(); template void ArchiveFile::parse(); @@ -1204,3 +1240,8 @@ template class elf::SharedFile; template class elf::SharedFile; template class elf::SharedFile; template class elf::SharedFile; + +template void elf::readJustSymbolsFile(MemoryBufferRef); +template void elf::readJustSymbolsFile(MemoryBufferRef); +template void elf::readJustSymbolsFile(MemoryBufferRef); +template void elf::readJustSymbolsFile(MemoryBufferRef); diff --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h index 5b9e9d7..862fde6 100644 --- a/lld/ELF/InputFiles.h +++ b/lld/ELF/InputFiles.h @@ -332,6 +332,9 @@ InputFile *createObjectFile(MemoryBufferRef MB, StringRef ArchiveName = "", uint64_t OffsetInArchive = 0); InputFile *createSharedFile(MemoryBufferRef MB, StringRef DefaultSoName); +// For --just-symbols +template void readJustSymbolsFile(MemoryBufferRef MB); + extern std::vector BinaryFiles; extern std::vector BitcodeFiles; extern std::vector ObjectFiles; diff --git a/lld/ELF/Options.td b/lld/ELF/Options.td index b1c7a1d..40b996e 100644 --- a/lld/ELF/Options.td +++ b/lld/ELF/Options.td @@ -180,6 +180,8 @@ defm image_base : Eq<"image-base">, HelpText<"Set the base address">; defm init: Eq<"init">, HelpText<"Specify an initializer function">, MetaVarName<"">; +defm just_symbols: Eq<"just-symbols">, HelpText<"Just link symbols">; + defm library: Eq<"library">, HelpText<"Root name of library to use">, MetaVarName<"">; diff --git a/lld/test/ELF/Inputs/just-symbols.s b/lld/test/ELF/Inputs/just-symbols.s new file mode 100644 index 0000000..922bbdf --- /dev/null +++ b/lld/test/ELF/Inputs/just-symbols.s @@ -0,0 +1,9 @@ +.globl foo, bar +foo: + ret + +.section .data +.type bar, @object +.size bar, 40 +bar: + .zero 40 diff --git a/lld/test/ELF/just-symbols.s b/lld/test/ELF/just-symbols.s new file mode 100644 index 0000000..d71345d --- /dev/null +++ b/lld/test/ELF/just-symbols.s @@ -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 -- 2.7.4