brintos

brintos / llvm-project-archived public Read only

0
0
Text · 5.5 KiB · ba4f95d Raw
174 lines · cpp
1//===- Symbols.cpp --------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "Symbols.h"10#include "COFFLinkerContext.h"11#include "InputFiles.h"12#include "lld/Common/ErrorHandler.h"13#include "llvm/Demangle/Demangle.h"14 15using namespace llvm;16using namespace llvm::object;17 18using namespace lld::coff;19 20namespace lld {21 22static_assert(sizeof(SymbolUnion) <= 48,23              "symbols should be optimized for memory usage");24 25// Returns a symbol name for an error message.26std::string maybeDemangleSymbol(const COFFLinkerContext &ctx,27                                StringRef symName) {28  if (ctx.config.demangle) {29    std::string prefix;30    StringRef prefixless = symName;31    if (prefixless.consume_front("__imp_"))32      prefix = "__declspec(dllimport) ";33    StringRef demangleInput = prefixless;34    if (ctx.config.machine == I386)35      demangleInput.consume_front("_");36    std::string demangled = demangle(demangleInput);37    if (demangled != demangleInput)38      return prefix + demangled;39    return (prefix + prefixless).str();40  }41  return std::string(symName);42}43std::string toString(const COFFLinkerContext &ctx, coff::Symbol &b) {44  return maybeDemangleSymbol(ctx, b.getName());45}46std::string toCOFFString(const COFFLinkerContext &ctx,47                         const Archive::Symbol &b) {48  return maybeDemangleSymbol(ctx, b.getName());49}50 51const COFFSyncStream &52coff::operator<<(const COFFSyncStream &s,53                 const llvm::object::Archive::Symbol *sym) {54  s << maybeDemangleSymbol(s.ctx, sym->getName());55  return s;56}57 58namespace coff {59 60void Symbol::computeName() {61  assert(nameData == nullptr &&62         "should only compute the name once for DefinedCOFF symbols");63  auto *d = cast<DefinedCOFF>(this);64  StringRef nameStr =65      check(cast<ObjFile>(d->file)->getCOFFObj()->getSymbolName(d->sym));66  nameData = nameStr.data();67  nameSize = nameStr.size();68  assert(nameSize == nameStr.size() && "name length truncated");69}70 71InputFile *Symbol::getFile() {72  if (auto *sym = dyn_cast<DefinedCOFF>(this))73    return sym->file;74  if (auto *sym = dyn_cast<LazyArchive>(this))75    return sym->file;76  if (auto *sym = dyn_cast<LazyObject>(this))77    return sym->file;78  if (auto *sym = dyn_cast<LazyDLLSymbol>(this))79    return sym->file;80  return nullptr;81}82 83bool Symbol::isLive() const {84  if (auto *r = dyn_cast<DefinedRegular>(this))85    return r->getChunk()->live;86  if (auto *imp = dyn_cast<DefinedImportData>(this))87    return imp->file->live;88  if (auto *imp = dyn_cast<DefinedImportThunk>(this))89    return imp->getChunk()->live;90  // Assume any other kind of symbol is live.91  return true;92}93 94Defined *Symbol::getDefined() {95  if (auto d = dyn_cast<Defined>(this))96    return d;97  if (auto u = dyn_cast<Undefined>(this))98    return u->getDefinedWeakAlias();99  return nullptr;100}101 102void Symbol::replaceKeepingName(Symbol *other, size_t size) {103  StringRef origName = getName();104  memcpy(this, other, size);105  nameData = origName.data();106  nameSize = origName.size();107}108 109COFFSymbolRef DefinedCOFF::getCOFFSymbol() {110  size_t symSize = cast<ObjFile>(file)->getCOFFObj()->getSymbolTableEntrySize();111  if (symSize == sizeof(coff_symbol16))112    return COFFSymbolRef(reinterpret_cast<const coff_symbol16 *>(sym));113  assert(symSize == sizeof(coff_symbol32));114  return COFFSymbolRef(reinterpret_cast<const coff_symbol32 *>(sym));115}116 117uint64_t DefinedAbsolute::getRVA() { return va - ctx.config.imageBase; }118 119DefinedImportThunk::DefinedImportThunk(COFFLinkerContext &ctx, StringRef name,120                                       DefinedImportData *s,121                                       ImportThunkChunk *chunk)122    : Defined(DefinedImportThunkKind, name), wrappedSym(s), data(chunk) {}123 124Symbol *Undefined::getWeakAlias() {125  // A weak alias may be a weak alias to another symbol, so check recursively.126  DenseSet<Symbol *> weakChain;127  for (Symbol *a = weakAlias; a; a = cast<Undefined>(a)->weakAlias) {128    // Anti-dependency symbols can't be chained.129    if (a->isAntiDep)130      break;131    if (!isa<Undefined>(a))132      return a;133    if (!weakChain.insert(a).second)134      break; // We have a cycle.135  }136  return nullptr;137}138 139bool Undefined::resolveWeakAlias() {140  Defined *d = getDefinedWeakAlias();141  if (!d)142    return false;143 144  // We want to replace Sym with D. However, we can't just blindly145  // copy sizeof(SymbolUnion) bytes from D to Sym because D may be an146  // internal symbol, and internal symbols are stored as "unparented"147  // Symbols. For that reason we need to check which type of symbol we148  // are dealing with and copy the correct number of bytes.149  StringRef name = getName();150  bool wasAntiDep = isAntiDep;151  if (isa<DefinedRegular>(d))152    memcpy(this, d, sizeof(DefinedRegular));153  else if (isa<DefinedAbsolute>(d))154    memcpy(this, d, sizeof(DefinedAbsolute));155  else156    memcpy(this, d, sizeof(SymbolUnion));157 158  nameData = name.data();159  nameSize = name.size();160  isAntiDep = wasAntiDep;161  return true;162}163 164MemoryBufferRef LazyArchive::getMemberBuffer() {165  Archive::Child c =166      CHECK(sym.getMember(), "could not get the member for symbol " +167                                 toCOFFString(file->symtab.ctx, sym));168  return CHECK(c.getMemoryBufferRef(),169               "could not get the buffer for the member defining symbol " +170                   toCOFFString(file->symtab.ctx, sym));171}172} // namespace coff173} // namespace lld174