llvm-project/lld/MachO/Symbols.cpp
Nico Weber 07ab597bb0 [lld/mac] Fix issues around thin archives
- most importantly, fix a use-after-free when using thin archives,
  by putting the archive unique_ptr to the arena allocator. This
  ports D65565 to MachO

- correctly demangle symbol namess from archives in diagnostics

- add a test for thin archives -- it finds this UaF, but only when
  running it under asan (it also finds the demangling fix)

- make forceLoadArchive() use addFile() with a bool to have the archive
  loading code in fewer places. no behavior change; matches COFF port a
  bit better

Differential Revision: https://reviews.llvm.org/D92360
2020-12-01 18:48:29 -05:00

54 lines
1.4 KiB
C++

//===- Symbols.cpp --------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "Symbols.h"
#include "InputFiles.h"
#include "SyntheticSections.h"
using namespace llvm;
using namespace lld;
using namespace lld::macho;
// Returns a symbol for an error message.
static std::string demangle(StringRef symName) {
if (config->demangle)
return demangleItanium(symName);
return std::string(symName);
}
std::string lld::toString(const Symbol &sym) {
return demangle(sym.getName());
}
std::string lld::toMachOString(const object::Archive::Symbol &b) {
return demangle(b.getName());
}
uint64_t Defined::getVA() const {
if (isAbsolute())
return value;
return isec->getVA() + value;
}
uint64_t Defined::getFileOffset() const {
if (isAbsolute()) {
error("absolute symbol " + toString(*this) +
" does not have a file offset");
return 0;
}
return isec->getFileOffset() + value;
}
void LazySymbol::fetchArchiveMember() { file->fetch(sym); }
uint64_t DSOHandle::getVA() const { return header->addr; }
uint64_t DSOHandle::getFileOffset() const { return header->fileOff; }
constexpr StringRef DSOHandle::name;