diff --git a/llvm/include/llvm/ObjCopy/CommonConfig.h b/llvm/include/llvm/ObjCopy/CommonConfig.h --- a/llvm/include/llvm/ObjCopy/CommonConfig.h +++ b/llvm/include/llvm/ObjCopy/CommonConfig.h @@ -18,6 +18,7 @@ #include "llvm/ADT/StringRef.h" #include "llvm/Object/ELFTypes.h" #include "llvm/Support/GlobPattern.h" +#include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/Regex.h" // Necessary for llvm::DebugCompressionType::None #include "llvm/Target/TargetOptions.h" @@ -186,6 +187,18 @@ std::vector BeforeSyms; }; +// Specify section name and section body for newly added or updated section. +struct NewSectionInfo { + NewSectionInfo() = default; + NewSectionInfo(StringRef Name, std::unique_ptr &Buffer) { + SectionName = Name; + SectionData.reset(Buffer.release()); + } + + StringRef SectionName; + std::shared_ptr SectionData; +}; + // Configuration for copying/stripping a single file. struct CommonConfig { // Main input/output options @@ -208,9 +221,9 @@ DiscardType DiscardMode = DiscardType::None; // Repeated options - std::vector AddSection; + std::vector AddSection; std::vector DumpSection; - std::vector UpdateSection; + std::vector UpdateSection; // Section matchers NameMatcher KeepSection; diff --git a/llvm/lib/ObjCopy/COFF/COFFObjcopy.cpp b/llvm/lib/ObjCopy/COFF/COFFObjcopy.cpp --- a/llvm/lib/ObjCopy/COFF/COFFObjcopy.cpp +++ b/llvm/lib/ObjCopy/COFF/COFFObjcopy.cpp @@ -230,56 +230,41 @@ It->second.NewFlags, Sec.Header.Characteristics); } - for (const auto &Flag : Config.AddSection) { - StringRef SecName, FileName; - std::tie(SecName, FileName) = Flag.split("="); - - auto BufOrErr = MemoryBuffer::getFile(FileName); - if (!BufOrErr) - return createFileError(FileName, errorCodeToError(BufOrErr.getError())); - auto Buf = std::move(*BufOrErr); - + for (const NewSectionInfo &NewSection : Config.AddSection) { uint32_t Characteristics; - const auto It = Config.SetSectionFlags.find(SecName); + const auto It = Config.SetSectionFlags.find(NewSection.SectionName); if (It != Config.SetSectionFlags.end()) Characteristics = flagsToCharacteristics(It->second.NewFlags, 0); else Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_ALIGN_1BYTES; - addSection( - Obj, SecName, - makeArrayRef(reinterpret_cast(Buf->getBufferStart()), - Buf->getBufferSize()), - Characteristics); + addSection(Obj, NewSection.SectionName, + makeArrayRef(reinterpret_cast( + NewSection.SectionData->getBufferStart()), + NewSection.SectionData->getBufferSize()), + Characteristics); } - for (StringRef Flag : Config.UpdateSection) { - StringRef SecName, FileName; - std::tie(SecName, FileName) = Flag.split('='); - - auto BufOrErr = MemoryBuffer::getFile(FileName); - if (!BufOrErr) - return createFileError(FileName, errorCodeToError(BufOrErr.getError())); - auto Buf = std::move(*BufOrErr); - - auto It = llvm::find_if(Obj.getMutableSections(), [SecName](auto &Sec) { - return Sec.Name == SecName; + for (const NewSectionInfo &NewSection : Config.UpdateSection) { + auto It = llvm::find_if(Obj.getMutableSections(), [&](auto &Sec) { + return Sec.Name == NewSection.SectionName; }); if (It == Obj.getMutableSections().end()) return createStringError(errc::invalid_argument, "could not find section with name '%s'", - SecName.str().c_str()); + NewSection.SectionName.str().c_str()); size_t ContentSize = It->getContents().size(); if (!ContentSize) return createStringError( errc::invalid_argument, "section '%s' cannot be updated because it does not have contents", - SecName.str().c_str()); - if (ContentSize < Buf->getBufferSize()) + NewSection.SectionName.str().c_str()); + if (ContentSize < NewSection.SectionData->getBufferSize()) return createStringError( errc::invalid_argument, "new section cannot be larger than previous section"); - It->setOwnedContents({Buf->getBufferStart(), Buf->getBufferEnd()}); + It->setOwnedContents({NewSection.SectionData->getBufferStart(), + NewSection.SectionData->getBufferEnd()}); } if (!Config.AddGnuDebugLink.empty()) diff --git a/llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp b/llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp --- a/llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp +++ b/llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp @@ -586,19 +586,12 @@ } static Error -handleUserSection(StringRef Flag, +handleUserSection(const NewSectionInfo &NewSection, function_ref)> F) { - std::pair SecPair = Flag.split("="); - StringRef SecName = SecPair.first; - StringRef File = SecPair.second; - ErrorOr> BufOrErr = MemoryBuffer::getFile(File); - if (!BufOrErr) - return createFileError(File, errorCodeToError(BufOrErr.getError())); - std::unique_ptr Buf = std::move(*BufOrErr); - ArrayRef Data( - reinterpret_cast(Buf->getBufferStart()), - Buf->getBufferSize()); - return F(SecName, Data); + ArrayRef Data(reinterpret_cast( + NewSection.SectionData->getBufferStart()), + NewSection.SectionData->getBufferSize()); + return F(NewSection.SectionName, Data); } // This function handles the high level operations of GNU objcopy including @@ -717,7 +710,7 @@ if (Sec.Flags & SHF_ALLOC && Sec.Type != SHT_NOTE) Sec.Type = SHT_NOBITS; - for (const auto &Flag : Config.AddSection) { + for (const NewSectionInfo &AddedSection : Config.AddSection) { auto AddSection = [&](StringRef Name, ArrayRef Data) { OwnedDataSection &NewSection = Obj.addSection(Name, Data); @@ -725,15 +718,15 @@ NewSection.Type = SHT_NOTE; return Error::success(); }; - if (Error E = handleUserSection(Flag, AddSection)) + if (Error E = handleUserSection(AddedSection, AddSection)) return E; } - for (StringRef Flag : Config.UpdateSection) { + for (const NewSectionInfo &NewSection : Config.UpdateSection) { auto UpdateSection = [&](StringRef Name, ArrayRef Data) { return Obj.updateSection(Name, Data); }; - if (Error E = handleUserSection(Flag, UpdateSection)) + if (Error E = handleUserSection(NewSection, UpdateSection)) return E; } diff --git a/llvm/lib/ObjCopy/MachO/MachOObjcopy.cpp b/llvm/lib/ObjCopy/MachO/MachOObjcopy.cpp --- a/llvm/lib/ObjCopy/MachO/MachOObjcopy.cpp +++ b/llvm/lib/ObjCopy/MachO/MachOObjcopy.cpp @@ -99,7 +99,7 @@ Sym.Name = std::string(I->getValue()); } - auto RemovePred = [Config, MachOConfig, + auto RemovePred = [&Config, &MachOConfig, &Obj](const std::unique_ptr &N) { if (N->Referenced) return false; @@ -283,17 +283,12 @@ SecName.str().c_str()); } -static Error addSection(StringRef SecName, StringRef Filename, Object &Obj) { - ErrorOr> BufOrErr = - MemoryBuffer::getFile(Filename); - if (!BufOrErr) - return createFileError(Filename, errorCodeToError(BufOrErr.getError())); - std::unique_ptr Buf = std::move(*BufOrErr); - - std::pair Pair = SecName.split(','); +static Error addSection(const NewSectionInfo &NewSection, Object &Obj) { + std::pair Pair = NewSection.SectionName.split(','); StringRef TargetSegName = Pair.first; Section Sec(TargetSegName, Pair.second); - Sec.Content = Obj.NewSectionsContents.save(Buf->getBuffer()); + Sec.Content = + Obj.NewSectionsContents.save(NewSection.SectionData->getBuffer()); Sec.Size = Sec.Content.size(); // Add the a section into an existing segment. @@ -342,24 +337,18 @@ return *FoundSec->get(); } -static Error updateSection(StringRef SecName, StringRef Filename, Object &O) { - Expected
SecToUpdateOrErr = findSection(SecName, O); +static Error updateSection(const NewSectionInfo &NewSection, Object &O) { + Expected
SecToUpdateOrErr = findSection(NewSection.SectionName, O); if (!SecToUpdateOrErr) return SecToUpdateOrErr.takeError(); Section &Sec = *SecToUpdateOrErr; - ErrorOr> BufOrErr = - MemoryBuffer::getFile(Filename); - if (!BufOrErr) - return createFileError(Filename, errorCodeToError(BufOrErr.getError())); - std::unique_ptr Buf = std::move(*BufOrErr); - - if (Buf->getBufferSize() > Sec.Size) + if (NewSection.SectionData->getBufferSize() > Sec.Size) return createStringError( errc::invalid_argument, "new section cannot be larger than previous section"); - Sec.Content = O.NewSectionsContents.save(Buf->getBuffer()); + Sec.Content = O.NewSectionsContents.save(NewSection.SectionData->getBuffer()); Sec.Size = Sec.Content.size(); return Error::success(); } @@ -411,23 +400,17 @@ for (std::unique_ptr
&Sec : LC.Sections) Sec->Relocations.clear(); - for (const auto &Flag : Config.AddSection) { - std::pair SecPair = Flag.split("="); - StringRef SecName = SecPair.first; - StringRef File = SecPair.second; - if (Error E = isValidMachOCannonicalName(SecName)) + for (const NewSectionInfo &NewSection : Config.AddSection) { + if (Error E = isValidMachOCannonicalName(NewSection.SectionName)) return E; - if (Error E = addSection(SecName, File, Obj)) + if (Error E = addSection(NewSection, Obj)) return E; } - for (const auto &Flag : Config.UpdateSection) { - StringRef SectionName; - StringRef FileName; - std::tie(SectionName, FileName) = Flag.split('='); - if (Error E = isValidMachOCannonicalName(SectionName)) + for (const NewSectionInfo &NewSection : Config.UpdateSection) { + if (Error E = isValidMachOCannonicalName(NewSection.SectionName)) return E; - if (Error E = updateSection(SectionName, FileName, Obj)) + if (Error E = updateSection(NewSection, Obj)) return E; } diff --git a/llvm/lib/ObjCopy/wasm/WasmObjcopy.cpp b/llvm/lib/ObjCopy/wasm/WasmObjcopy.cpp --- a/llvm/lib/ObjCopy/wasm/WasmObjcopy.cpp +++ b/llvm/lib/ObjCopy/wasm/WasmObjcopy.cpp @@ -121,21 +121,19 @@ removeSections(Config, Obj); - for (StringRef Flag : Config.AddSection) { - StringRef SecName, FileName; - std::tie(SecName, FileName) = Flag.split("="); - ErrorOr> BufOrErr = - MemoryBuffer::getFile(FileName); - if (!BufOrErr) - return createFileError(FileName, errorCodeToError(BufOrErr.getError())); + for (const NewSectionInfo &NewSection : Config.AddSection) { Section Sec; Sec.SectionType = llvm::wasm::WASM_SEC_CUSTOM; - Sec.Name = SecName; - std::unique_ptr Buf = std::move(*BufOrErr); + Sec.Name = NewSection.SectionName; + + std::unique_ptr BufferCopy = MemoryBuffer::getMemBufferCopy( + NewSection.SectionData->getBufferStart(), + NewSection.SectionData->getBufferIdentifier()); Sec.Contents = makeArrayRef( - reinterpret_cast(Buf->getBufferStart()), - Buf->getBufferSize()); - Obj.addSectionWithOwnedContents(Sec, std::move(Buf)); + reinterpret_cast(BufferCopy->getBufferStart()), + BufferCopy->getBufferSize()); + + Obj.addSectionWithOwnedContents(Sec, std::move(BufferCopy)); } return Error::success(); diff --git a/llvm/test/tools/llvm-objcopy/COFF/add-section.test b/llvm/test/tools/llvm-objcopy/COFF/add-section.test --- a/llvm/test/tools/llvm-objcopy/COFF/add-section.test +++ b/llvm/test/tools/llvm-objcopy/COFF/add-section.test @@ -61,7 +61,7 @@ ## to be added does not exist. # RUN: not llvm-objcopy --add-section=.another.section=%t2 %t %t3 2>&1 | FileCheck -DFILE1=%t -DFILE2=%t2 -DMSG=%errc_ENOENT %s --check-prefixes=ERR1 -# ERR1: error: '[[FILE1]]': '[[FILE2]]': [[MSG]] +# ERR1: llvm-objcopy: error: '[[FILE2]]': [[MSG]] ## Another negative test for invalid --add-sections command line argument. # RUN: not llvm-objcopy --add-section=.another.section %t %t3 2>&1 | FileCheck %s --check-prefixes=ERR2 diff --git a/llvm/test/tools/llvm-objcopy/ELF/add-section.test b/llvm/test/tools/llvm-objcopy/ELF/add-section.test --- a/llvm/test/tools/llvm-objcopy/ELF/add-section.test +++ b/llvm/test/tools/llvm-objcopy/ELF/add-section.test @@ -51,7 +51,7 @@ ## to be added does not exist. # RUN: not llvm-objcopy --add-section=.section.name=%t.missing %t %t.out 2>&1 | FileCheck -DFILE1=%t -DFILE2=%t.missing -DMSG=%errc_ENOENT %s --check-prefixes=ERR1 -# ERR1: error: '[[FILE1]]': '[[FILE2]]': [[MSG]] +# ERR1: llvm-objcopy: error: '[[FILE2]]': [[MSG]] ## Negative test for invalid --add-sections argument - missing '='. # RUN: not llvm-objcopy --add-section=.section.name %t %t.out 2>&1 | FileCheck %s --check-prefixes=ERR2 diff --git a/llvm/test/tools/llvm-objcopy/MachO/add-section-error.test b/llvm/test/tools/llvm-objcopy/MachO/add-section-error.test --- a/llvm/test/tools/llvm-objcopy/MachO/add-section-error.test +++ b/llvm/test/tools/llvm-objcopy/MachO/add-section-error.test @@ -6,7 +6,7 @@ ## Error case 1: Nonexistent input file is specified by --add-section. # RUN: not llvm-objcopy --add-section __TEXT,__text=%t.missing %t %t.nonexistent-file 2>&1 \ # RUN: | FileCheck %s -DINPUT=%t -DSECTION_DATA_FILE=%t.missing -DMSG=%errc_ENOENT --check-prefix=NONEXSITENT-FILE -# NONEXSITENT-FILE: error: '[[INPUT]]': '[[SECTION_DATA_FILE]]': [[MSG]] +# NONEXSITENT-FILE: llvm-objcopy: error: '[[SECTION_DATA_FILE]]': [[MSG]] ## Error case 2: Too long segment name. # RUN: not llvm-objcopy --add-section __TOOOOOOOOO_LONG,__text=%t.data %t %t.too-long-seg-name 2>&1 \ diff --git a/llvm/test/tools/llvm-objcopy/MachO/update-section.test b/llvm/test/tools/llvm-objcopy/MachO/update-section.test --- a/llvm/test/tools/llvm-objcopy/MachO/update-section.test +++ b/llvm/test/tools/llvm-objcopy/MachO/update-section.test @@ -26,7 +26,7 @@ # RUN: llvm-objcopy --update-section __TEXT,__text=%t.diff %t - | obj2yaml | FileCheck %s --check-prefix=FULL-SECNAME # FULL-SECNAME: content: '41414142' -# RUN: not llvm-objcopy --update-section __text=%t.dff %t /dev/null 2>&1 | FileCheck %s --check-prefix=NON-CANONICAL-SECNAME +# RUN: not llvm-objcopy --update-section __text=%t.diff %t /dev/null 2>&1 | FileCheck %s --check-prefix=NON-CANONICAL-SECNAME # NON-CANONICAL-SECNAME: error: {{.*}}invalid section name '__text' (should be formatted as ',
') --- !mach-o diff --git a/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp b/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp --- a/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp +++ b/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp @@ -852,22 +852,38 @@ if (!ArgValue.contains('=')) return createStringError(errc::invalid_argument, "bad format for --add-section: missing '='"); - if (ArgValue.split("=").second.empty()) + std::pair SecPair = ArgValue.split("="); + if (SecPair.second.empty()) return createStringError( errc::invalid_argument, "bad format for --add-section: missing file name"); - Config.AddSection.push_back(ArgValue); + + ErrorOr> BufOrErr = + MemoryBuffer::getFile(SecPair.second); + if (!BufOrErr) + return createFileError(SecPair.second, + errorCodeToError(BufOrErr.getError())); + + Config.AddSection.push_back({SecPair.first, *BufOrErr}); } for (auto Arg : InputArgs.filtered(OBJCOPY_update_section)) { StringRef ArgValue(Arg->getValue()); if (!ArgValue.contains('=')) return createStringError(errc::invalid_argument, "bad format for --update-section: missing '='"); - if (ArgValue.split("=").second.empty()) + std::pair SecPair = ArgValue.split("="); + if (SecPair.second.empty()) return createStringError( errc::invalid_argument, "bad format for --update-section: missing file name"); - Config.UpdateSection.push_back(ArgValue); + + ErrorOr> BufOrErr = + MemoryBuffer::getFile(SecPair.second); + if (!BufOrErr) + return createFileError(SecPair.second, + errorCodeToError(BufOrErr.getError())); + + Config.UpdateSection.push_back({SecPair.first, *BufOrErr}); } for (auto *Arg : InputArgs.filtered(OBJCOPY_dump_section)) { StringRef Value(Arg->getValue()); diff --git a/llvm/unittests/ObjCopy/ObjCopyTest.cpp b/llvm/unittests/ObjCopy/ObjCopyTest.cpp --- a/llvm/unittests/ObjCopy/ObjCopyTest.cpp +++ b/llvm/unittests/ObjCopy/ObjCopyTest.cpp @@ -116,3 +116,115 @@ )", [](const Binary &File) { return File.isWasm(); }); } + +TEST(AddUpdateDebugSection, ELF) { + const char *YamlCreationString = R"( +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL)"; + + // Create Object file from YAML description. + SmallVector Storage; + std::unique_ptr Obj = yaml2ObjectFile( + Storage, YamlCreationString, [](const Twine &Err) { errs() << Err; }); + ASSERT_TRUE(Obj); + ASSERT_TRUE(Obj->isELF()); + + // Create .debug_abbrev section to add. + // Contents of .debug_abbrev + //.byte 1 # Abbreviation Code + //.byte 17 # DW_TAG_compile_unit + //.byte 0 # DW_CHILDREN_no + const char *AbbrevDataAdd = "\1\17\0"; + uint64_t AbbrevDataAddSize = 3; + std::unique_ptr SectionDataToAdd = MemoryBuffer::getMemBuffer( + StringRef(AbbrevDataAdd, AbbrevDataAddSize), ".debug_abbrev", false); + + ConfigManager Config; + Config.Common.OutputFilename = "a.out"; + Config.Common.AddSection.push_back({".debug_abbrev", SectionDataToAdd}); + + // Call executeObjcopyOnBinary() + SmallVector DataVector; + raw_svector_ostream OutStream(DataVector); + Error Err = objcopy::executeObjcopyOnBinary(Config, *Obj.get(), OutStream); + ASSERT_FALSE(std::move(Err)); + + MemoryBufferRef Buffer(StringRef(DataVector.data(), DataVector.size()), + Config.Common.OutputFilename); + + // Create binary for copied file. + Expected> AddObjOrErr = createBinary(Buffer); + ASSERT_THAT_EXPECTED(AddObjOrErr, Succeeded()); + ASSERT_TRUE((*AddObjOrErr)->isObject()); + ASSERT_TRUE((*AddObjOrErr)->isELF()); + + // Check that copied file has ".debug_abbrev" section. + bool HasAddedAbbrevSection = false; + for (const object::SectionRef &Sect : + static_cast((*AddObjOrErr).get())->sections()) { + Expected SectNameOrErr = Sect.getName(); + ASSERT_THAT_EXPECTED(SectNameOrErr, Succeeded()); + + if (*SectNameOrErr == ".debug_abbrev") { + HasAddedAbbrevSection = true; + Expected SectionData = Sect.getContents(); + ASSERT_THAT_EXPECTED(SectionData, Succeeded()); + ASSERT_TRUE(Sect.getSize() == AbbrevDataAddSize); + ASSERT_TRUE( + memcmp(SectionData->data(), AbbrevDataAdd, AbbrevDataAddSize) == 0); + } + } + ASSERT_TRUE(HasAddedAbbrevSection); + + // Create .debug_abbrev section to update. + // Contents of .debug_abbrev + //.byte 1 # Abbreviation Code + //.byte 0 # + const char *AbbrevDataUpdate = "\1\0"; + uint64_t AbbrevDataUpdateSize = 2; + std::unique_ptr SectionDataToUpdate = + MemoryBuffer::getMemBuffer( + StringRef(AbbrevDataUpdate, AbbrevDataUpdateSize), ".debug_abbrev", + false); + + Config.Common.AddSection.clear(); + Config.Common.UpdateSection.push_back({".debug_abbrev", SectionDataToUpdate}); + + // Call executeObjcopyOnBinary() + SmallVector DataVectorUpd; + raw_svector_ostream OutStreamUpd(DataVectorUpd); + Err = + objcopy::executeObjcopyOnBinary(Config, *AddObjOrErr.get(), OutStreamUpd); + ASSERT_FALSE(std::move(Err)); + + Buffer = + MemoryBufferRef(StringRef(DataVectorUpd.data(), DataVectorUpd.size()), + Config.Common.OutputFilename); + + // Create binary for copied file. + Expected> UpdateObjOrErr = createBinary(Buffer); + ASSERT_THAT_EXPECTED(UpdateObjOrErr, Succeeded()); + ASSERT_TRUE((*UpdateObjOrErr)->isObject()); + ASSERT_TRUE((*UpdateObjOrErr)->isELF()); + + // Check that copied file has updated ".debug_abbrev" section. + bool HasUpdatedAbbrevSection = false; + for (const object::SectionRef &Sect : + static_cast((*UpdateObjOrErr).get())->sections()) { + Expected SectNameOrErr = Sect.getName(); + ASSERT_THAT_EXPECTED(SectNameOrErr, Succeeded()); + + if (*SectNameOrErr == ".debug_abbrev") { + Expected SectionData = Sect.getContents(); + ASSERT_THAT_EXPECTED(SectionData, Succeeded()); + ASSERT_TRUE(Sect.getSize() == AbbrevDataUpdateSize); + ASSERT_TRUE(memcmp(SectionData->data(), AbbrevDataUpdate, + AbbrevDataUpdateSize) == 0); + HasUpdatedAbbrevSection = true; + } + } + ASSERT_TRUE(HasUpdatedAbbrevSection); +}