Skip to content

Commit

Permalink
Merged master:ababcd2ab491 into amd-gfx:b3784523579f
Browse files Browse the repository at this point in the history
Local branch amd-gfx b378452 Merged master:5811d723998a into amd-gfx:f8c9b6a1d1e1
Remote branch master ababcd2 [llvm-objcopy][NFC] refactor error handling. part 2.
  • Loading branch information
Sw authored and Sw committed Sep 27, 2020
2 parents b378452 + ababcd2 commit 060b66d
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 22 deletions.
17 changes: 16 additions & 1 deletion lldb/source/Plugins/ABI/Mips/ABISysV_mips.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ enum dwarf_regnums {
dwarf_pc
};

static const RegisterInfo g_register_infos[] = {
static RegisterInfo g_register_infos[] = {
// NAME ALT SZ OFF ENCODING FORMAT EH_FRAME
// DWARF GENERIC PROCESS PLUGINS
// LLDB NATIVE VALUE REGS INVALIDATE REGS
Expand Down Expand Up @@ -542,9 +542,24 @@ static const RegisterInfo g_register_infos[] = {

static const uint32_t k_num_register_infos =
llvm::array_lengthof(g_register_infos);
static bool g_register_info_names_constified = false;

const lldb_private::RegisterInfo *
ABISysV_mips::GetRegisterInfoArray(uint32_t &count) {
// Make the C-string names and alt_names for the register infos into const
// C-string values by having the ConstString unique the names in the global
// constant C-string pool.
if (!g_register_info_names_constified) {
g_register_info_names_constified = true;
for (uint32_t i = 0; i < k_num_register_infos; ++i) {
if (g_register_infos[i].name)
g_register_infos[i].name =
ConstString(g_register_infos[i].name).GetCString();
if (g_register_infos[i].alt_name)
g_register_infos[i].alt_name =
ConstString(g_register_infos[i].alt_name).GetCString();
}
}
count = k_num_register_infos;
return g_register_infos;
}
Expand Down
17 changes: 16 additions & 1 deletion lldb/source/Plugins/ABI/Mips/ABISysV_mips64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ enum dwarf_regnums {
dwarf_pc
};

static const RegisterInfo g_register_infos_mips64[] = {
static RegisterInfo g_register_infos_mips64[] = {
// NAME ALT SZ OFF ENCODING FORMAT EH_FRAME
// DWARF GENERIC PROCESS PLUGIN
// LLDB NATIVE
Expand Down Expand Up @@ -542,9 +542,24 @@ static const RegisterInfo g_register_infos_mips64[] = {

static const uint32_t k_num_register_infos =
llvm::array_lengthof(g_register_infos_mips64);
static bool g_register_info_names_constified = false;

const lldb_private::RegisterInfo *
ABISysV_mips64::GetRegisterInfoArray(uint32_t &count) {
// Make the C-string names and alt_names for the register infos into const
// C-string values by having the ConstString unique the names in the global
// constant C-string pool.
if (!g_register_info_names_constified) {
g_register_info_names_constified = true;
for (uint32_t i = 0; i < k_num_register_infos; ++i) {
if (g_register_infos_mips64[i].name)
g_register_infos_mips64[i].name =
ConstString(g_register_infos_mips64[i].name).GetCString();
if (g_register_infos_mips64[i].alt_name)
g_register_infos_mips64[i].alt_name =
ConstString(g_register_infos_mips64[i].alt_name).GetCString();
}
}
count = k_num_register_infos;
return g_register_infos_mips64;
}
Expand Down
37 changes: 23 additions & 14 deletions llvm/tools/llvm-objcopy/COFF/COFFObjcopy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ static uint64_t getNextRVA(const Object &Obj) {
Obj.IsPE ? Obj.PeHeader.SectionAlignment : 1);
}

static std::vector<uint8_t> createGnuDebugLinkSectionContents(StringRef File) {
static Expected<std::vector<uint8_t>>
createGnuDebugLinkSectionContents(StringRef File) {
ErrorOr<std::unique_ptr<MemoryBuffer>> LinkTargetOrErr =
MemoryBuffer::getFile(File);
if (!LinkTargetOrErr)
error("'" + File + "': " + LinkTargetOrErr.getError().message());
return createFileError(File, LinkTargetOrErr.getError());
auto LinkTarget = std::move(*LinkTargetOrErr);
uint32_t CRC32 = llvm::crc32(arrayRefFromStringRef(LinkTarget->getBuffer()));

Expand Down Expand Up @@ -81,12 +82,17 @@ static void addSection(Object &Obj, StringRef Name, ArrayRef<uint8_t> Contents,
Obj.addSections(Sec);
}

static void addGnuDebugLink(Object &Obj, StringRef DebugLinkFile) {
std::vector<uint8_t> Contents =
static Error addGnuDebugLink(Object &Obj, StringRef DebugLinkFile) {
Expected<std::vector<uint8_t>> Contents =
createGnuDebugLinkSectionContents(DebugLinkFile);
addSection(Obj, ".gnu_debuglink", Contents,
if (!Contents)
return Contents.takeError();

addSection(Obj, ".gnu_debuglink", *Contents,
IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ |
IMAGE_SCN_MEM_DISCARDABLE);

return Error::success();
}

static void setSectionFlags(Section &Sec, SectionFlag AllFlags) {
Expand Down Expand Up @@ -174,8 +180,7 @@ static Error handleArgs(const CopyConfig &Config, Object &Obj) {
Sym.Name = I->getValue();
}

// Actually do removals of symbols.
Obj.removeSymbols([&](const Symbol &Sym) {
auto ToRemove = [&](const Symbol &Sym) -> Expected<bool> {
// For StripAll, all relocations have been stripped and we remove all
// symbols.
if (Config.StripAll || Config.StripAllGNU)
Expand All @@ -184,11 +189,10 @@ static Error handleArgs(const CopyConfig &Config, Object &Obj) {
if (Config.SymbolsToRemove.matches(Sym.Name)) {
// Explicitly removing a referenced symbol is an error.
if (Sym.Referenced)
reportError(Config.OutputFilename,
createStringError(llvm::errc::invalid_argument,
"not stripping symbol '%s' because it is "
"named in a relocation",
Sym.Name.str().c_str()));
return createStringError(
llvm::errc::invalid_argument,
"'" + Config.OutputFilename + "': not stripping symbol '" +
Sym.Name.str() + "' because it is named in a relocation");
return true;
}

Expand All @@ -213,7 +217,11 @@ static Error handleArgs(const CopyConfig &Config, Object &Obj) {
}

return false;
});
};

// Actually do removals of symbols.
if (Error Err = Obj.removeSymbols(ToRemove))
return Err;

if (!Config.SetSectionFlags.empty())
for (Section &Sec : Obj.getMutableSections()) {
Expand All @@ -239,7 +247,8 @@ static Error handleArgs(const CopyConfig &Config, Object &Obj) {
}

if (!Config.AddGnuDebugLink.empty())
addGnuDebugLink(Obj, Config.AddGnuDebugLink);
if (Error E = addGnuDebugLink(Obj, Config.AddGnuDebugLink))
return E;

if (Config.AllowBrokenLinks || !Config.BuildIdLinkDir.empty() ||
Config.BuildIdLinkInput || Config.BuildIdLinkOutput ||
Expand Down
21 changes: 16 additions & 5 deletions llvm/tools/llvm-objcopy/COFF/Object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,23 @@ const Symbol *Object::findSymbol(size_t UniqueId) const {
return It->second;
}

void Object::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) {
Symbols.erase(
std::remove_if(std::begin(Symbols), std::end(Symbols),
[ToRemove](const Symbol &Sym) { return ToRemove(Sym); }),
std::end(Symbols));
Error Object::removeSymbols(
function_ref<Expected<bool>(const Symbol &)> ToRemove) {
Error Errs = Error::success();
Symbols.erase(std::remove_if(std::begin(Symbols), std::end(Symbols),
[ToRemove, &Errs](const Symbol &Sym) {
Expected<bool> ShouldRemove = ToRemove(Sym);
if (!ShouldRemove) {
Errs = joinErrors(std::move(Errs),
ShouldRemove.takeError());
return false;
}
return *ShouldRemove;
}),
std::end(Symbols));

updateSymbols();
return Errs;
}

Error Object::markSymbols() {
Expand Down
2 changes: 1 addition & 1 deletion llvm/tools/llvm-objcopy/COFF/Object.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ struct Object {
const Symbol *findSymbol(size_t UniqueId) const;

void addSymbols(ArrayRef<Symbol> NewSymbols);
void removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
Error removeSymbols(function_ref<Expected<bool>(const Symbol &)> ToRemove);

// Set the Referenced field on all Symbols, based on relocations in
// all sections.
Expand Down

0 comments on commit 060b66d

Please sign in to comment.