Index: test/tools/llvm-objcopy/ELF/change-entry-point.test =================================================================== --- test/tools/llvm-objcopy/ELF/change-entry-point.test +++ test/tools/llvm-objcopy/ELF/change-entry-point.test @@ -0,0 +1,41 @@ +# RUN: yaml2obj %s -o %t +# RUN: llvm-objcopy --set-start 0x1000 %t %t2 +# RUN: llvm-readobj --file-headers %t2 | FileCheck %s --check-prefix=SET +# RUN: llvm-objcopy --set-start 4096 %t %t3 +# RUN: cmp %t2 %t3 +# RUN: llvm-objcopy --change-start 0x50 %t %t4 +# RUN: llvm-readobj --file-headers %t4 | FileCheck %s --check-prefix=ADD +# RUN: llvm-objcopy --adjust-start -128 %t %t5 +# RUN: llvm-readobj --file-headers %t5 | FileCheck %s --check-prefix=SUB +# RUN: not llvm-objcopy --set-start -123 %t %t6 2>&1 | FileCheck %s --check-prefix=SET-ERR +# RUN: llvm-objcopy --set-start 0x100000000 %t %t7 +# RUN: llvm-readobj --file-headers %t7 | FileCheck %s --check-prefix=SET-LARGE +# RUN: llvm-objcopy --change-start 0x100000000 %t %t8 +# RUN: llvm-readobj --file-headers %t8 | FileCheck %s --check-prefix=ADD-LARGE +# RUN: not llvm-objcopy --change-start -xyz %t %t9 2>&1 | FileCheck %s --check-prefix=ADD-ERR +# RUN: llvm-objcopy --change-start -4353 %t %t10 +# RUN: llvm-readobj --file-headers %t10 | FileCheck %s --check-prefix=ADD-UNDERFLOW + +!ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_EXEC + Machine: EM_X86_64 + Entry: 0x1100 +Sections: + - Name: .text + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC, SHF_EXECINSTR ] + Address: 0x1000 + AddressAlign: 0x0000000000001000 + Size: 0x200 + +# SET: Entry: 0x1000 +# ADD: Entry: 0x1150 +# SUB: Entry: 0x1080 +# SET-ERR: error: bad entry point address: '-123' +# SET-LARGE: Entry: 0x100000000 +# ADD-LARGE: Entry: 0x100001100 +# ADD-ERR: error: bad entry point increment: '-xyz' +# ADD-UNDERFLOW: Entry: 0xFFFFFFFFFFFFFFFF Index: tools/llvm-objcopy/COFF/COFFObjcopy.cpp =================================================================== --- tools/llvm-objcopy/COFF/COFFObjcopy.cpp +++ tools/llvm-objcopy/COFF/COFFObjcopy.cpp @@ -185,7 +185,7 @@ Config.ExtractDWO || Config.KeepFileSymbols || Config.LocalizeHidden || Config.PreserveDates || Config.StripDWO || Config.StripNonAlloc || Config.StripSections || Config.Weaken || Config.DecompressDebugSections || - Config.DiscardMode == DiscardType::Locals) { + Config.DiscardMode == DiscardType::Locals || Config.EntryExpr) { return createStringError(llvm::errc::invalid_argument, "Option not supported by llvm-objcopy for COFF"); } Index: tools/llvm-objcopy/CopyConfig.h =================================================================== --- tools/llvm-objcopy/CopyConfig.h +++ tools/llvm-objcopy/CopyConfig.h @@ -101,6 +101,12 @@ StringMap SetSectionFlags; StringMap SymbolsToRename; + // ELF entry point address expression. Input parameter is an entry point + // address in input ELF file. Entry address in output file is calculated + // with EntryExpr(input_address), when either --set-start or --change-start + // is used. + std::function EntryExpr; + // Boolean options bool DeterministicArchives = true; bool ExtractDWO = false; Index: tools/llvm-objcopy/CopyConfig.cpp =================================================================== --- tools/llvm-objcopy/CopyConfig.cpp +++ tools/llvm-objcopy/CopyConfig.cpp @@ -282,6 +282,15 @@ } return Error::success(); } + +template +static T getAsIntegerOrFail(StringRef Val, StringRef Err) { + T Result; + if (Val.getAsInteger(0, Result)) + error(Err + ": '" + Val + "'"); + return Result; +} + // ParseObjcopyOptions returns the config and sets the input arguments. If a // help flag is set then ParseObjcopyOptions will print the help messege and // exit. @@ -482,6 +491,18 @@ Config.PreserveDates = InputArgs.hasArg(OBJCOPY_preserve_dates); + if (auto *Arg = InputArgs.getLastArg(OBJCOPY_set_start)) { + auto EAddr = getAsIntegerOrFail(Arg->getValue(), + "bad entry point address"); + Config.EntryExpr = [EAddr](uint64_t) { return EAddr; }; + } + + if (auto *Arg = InputArgs.getLastArg(OBJCOPY_change_start)) { + auto EIncr = getAsIntegerOrFail(Arg->getValue(), + "bad entry point increment"); + Config.EntryExpr = [EIncr](uint64_t EAddr) { return EAddr + EIncr; }; + } + if (Config.DecompressDebugSections && Config.CompressionType != DebugCompressionType::None) { error("Cannot specify --compress-debug-sections at the same time as " Index: tools/llvm-objcopy/ELF/ELFObjcopy.cpp =================================================================== --- tools/llvm-objcopy/ELF/ELFObjcopy.cpp +++ tools/llvm-objcopy/ELF/ELFObjcopy.cpp @@ -566,6 +566,8 @@ if (!Config.AddGnuDebugLink.empty()) Obj.addSection(Config.AddGnuDebugLink); + if (Config.EntryExpr) + Obj.Entry = Config.EntryExpr(Obj.Entry); return Error::success(); } Index: tools/llvm-objcopy/ObjcopyOpts.td =================================================================== --- tools/llvm-objcopy/ObjcopyOpts.td +++ tools/llvm-objcopy/ObjcopyOpts.td @@ -237,3 +237,10 @@ def regex : Flag<["-", "--"], "regex">, HelpText<"Permit regular expressions in name comparison">; + +defm set_start : Eq<"set-start", "Set the start address to ">, + MetaVarName<"addr">; +defm change_start : Eq<"change-start", "Add to the start address">, + MetaVarName<"incr">; +def adjust_start : JoinedOrSeparate<["-", "--"], "adjust-start">, + Alias;