arda updated this revision to Diff 453550. arda added a comment. Use obj2yaml and yaml2obj to avoid unreadable object files
Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D131618/new/ https://reviews.llvm.org/D131618 Files: clang/include/clang/Basic/CodeGenOptions.def clang/include/clang/CodeGen/BackendUtil.h clang/include/clang/Driver/Options.td clang/include/clang/Driver/ToolChain.h clang/lib/CodeGen/BackendUtil.cpp clang/lib/CodeGen/CodeGenAction.cpp clang/lib/Driver/Driver.cpp clang/lib/Driver/ToolChains/Clang.cpp lld/ELF/Config.h lld/ELF/Driver.cpp lld/ELF/InputFiles.cpp lld/ELF/Options.td lld/test/ELF/fatlto/Inputs/a-fatLTO.yaml lld/test/ELF/fatlto/Inputs/a-thinLTO.ll lld/test/ELF/fatlto/Inputs/a.c lld/test/ELF/fatlto/Inputs/a.h lld/test/ELF/fatlto/Inputs/a.yaml lld/test/ELF/fatlto/Inputs/main-fatLTO.yaml lld/test/ELF/fatlto/Inputs/main-thinLTO.ll lld/test/ELF/fatlto/Inputs/main.c lld/test/ELF/fatlto/Inputs/main.yaml lld/test/ELF/fatlto/fatlto.test llvm/include/llvm/Bitcode/BitcodeWriter.h llvm/lib/Bitcode/Writer/BitcodeWriter.cpp llvm/lib/Object/ObjectFile.cpp
Index: llvm/lib/Object/ObjectFile.cpp =================================================================== --- llvm/lib/Object/ObjectFile.cpp +++ llvm/lib/Object/ObjectFile.cpp @@ -79,7 +79,7 @@ bool ObjectFile::isSectionBitcode(DataRefImpl Sec) const { Expected<StringRef> NameOrErr = getSectionName(Sec); if (NameOrErr) - return *NameOrErr == ".llvmbc"; + return *NameOrErr == ".llvmbc" || *NameOrErr == ".llvm.lto"; consumeError(NameOrErr.takeError()); return false; } Index: llvm/lib/Bitcode/Writer/BitcodeWriter.cpp =================================================================== --- llvm/lib/Bitcode/Writer/BitcodeWriter.cpp +++ llvm/lib/Bitcode/Writer/BitcodeWriter.cpp @@ -4953,6 +4953,37 @@ llvm_unreachable("Unimplemented ObjectFormatType"); } +static const char *getSectionNameForBitcodeForFatLTO(const Triple &T) { + switch (T.getObjectFormat()) { + case Triple::MachO: + llvm::report_fatal_error("MachO is not yet implemented for FatLTO"); + break; + case Triple::COFF: + llvm::report_fatal_error("COFF is not yet implemented for FatLTO"); + break; + case Triple::ELF: + return ".llvm.lto"; + case Triple::Wasm: + llvm::report_fatal_error("Wasm is not yet implemented for FatLTO"); + break; + case Triple::UnknownObjectFormat: + return ".llvm.lto"; + case Triple::GOFF: + llvm::report_fatal_error("GOFF is not yet implemented for FatLTO"); + break; + case Triple::SPIRV: + llvm::report_fatal_error("SPIRV is not yet implemented for FatLTO"); + break; + case Triple::XCOFF: + llvm::report_fatal_error("XCOFF is not yet implemented for FatLTO"); + break; + case Triple::DXContainer: + llvm::report_fatal_error("DXContainer is not yet implemented for FatLTO"); + break; + } + llvm_unreachable("Unimplemented ObjectFormatType"); +} + void llvm::embedBitcodeInModule(llvm::Module &M, llvm::MemoryBufferRef Buf, bool EmbedBitcode, bool EmbedCmdline, const std::vector<uint8_t> &CmdArgs) { @@ -5045,3 +5076,68 @@ llvm::ConstantArray::get(ATy, UsedArray), "llvm.compiler.used"); NewUsed->setSection("llvm.metadata"); } + +void llvm::embedBitcodeInFatObject(llvm::Module &M, llvm::MemoryBufferRef Buf) { + // Save llvm.compiler.used and remove it. + SmallVector<Constant *, 2> UsedArray; + SmallVector<GlobalValue *, 4> UsedGlobals; + Type *UsedElementType = Type::getInt8Ty(M.getContext())->getPointerTo(0); + GlobalVariable *Used = collectUsedGlobalVariables(M, UsedGlobals, true); + for (auto *GV : UsedGlobals) { + if (GV->getName() != "llvm.embedded.module" && + GV->getName() != "llvm.cmdline") + UsedArray.push_back( + ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, UsedElementType)); + } + if (Used) + Used->eraseFromParent(); + + // Embed the bitcode for the llvm module. + std::string Data; + ArrayRef<uint8_t> ModuleData; + Triple T(M.getTargetTriple()); + + if (Buf.getBufferSize() == 0 || + !isBitcode((const unsigned char *)Buf.getBufferStart(), + (const unsigned char *)Buf.getBufferEnd())) { + // If the input is LLVM Assembly, bitcode is produced by serializing + // the module. Use-lists order need to be preserved in this case. + llvm::raw_string_ostream OS(Data); + llvm::WriteBitcodeToFile(M, OS, /* ShouldPreserveUseListOrder */ true); + ModuleData = + ArrayRef<uint8_t>((const uint8_t *)OS.str().data(), OS.str().size()); + } else + // If the input is LLVM bitcode, write the input byte stream directly. + ModuleData = ArrayRef<uint8_t>((const uint8_t *)Buf.getBufferStart(), + Buf.getBufferSize()); + llvm::Constant *ModuleConstant = + llvm::ConstantDataArray::get(M.getContext(), ModuleData); + llvm::GlobalVariable *GV = new llvm::GlobalVariable( + M, ModuleConstant->getType(), true, llvm::GlobalValue::PrivateLinkage, + ModuleConstant); + GV->setSection(getSectionNameForBitcodeForFatLTO(T)); + // Set alignment to 1 to prevent padding between two contributions from input + // sections after linking. + GV->setAlignment(Align(1)); + UsedArray.push_back( + ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, UsedElementType)); + if (llvm::GlobalVariable *Old = + M.getGlobalVariable("llvm.embedded.module", true)) { + assert(Old->hasZeroLiveUses() && + "llvm.embedded.module can only be used once in llvm.compiler.used"); + GV->takeName(Old); + Old->eraseFromParent(); + } else { + GV->setName("llvm.embedded.module"); + } + + if (UsedArray.empty()) + return; + + // Recreate llvm.compiler.used. + ArrayType *ATy = ArrayType::get(UsedElementType, UsedArray.size()); + auto *NewUsed = new GlobalVariable( + M, ATy, false, llvm::GlobalValue::AppendingLinkage, + llvm::ConstantArray::get(ATy, UsedArray), "llvm.compiler.used"); + NewUsed->setSection("llvm.metadata"); +} Index: llvm/include/llvm/Bitcode/BitcodeWriter.h =================================================================== --- llvm/include/llvm/Bitcode/BitcodeWriter.h +++ llvm/include/llvm/Bitcode/BitcodeWriter.h @@ -165,6 +165,7 @@ bool EmbedCmdline, const std::vector<uint8_t> &CmdArgs); + void embedBitcodeInFatObject(Module &M, MemoryBufferRef Buf); } // end namespace llvm #endif // LLVM_BITCODE_BITCODEWRITER_H Index: lld/test/ELF/fatlto/fatlto.test =================================================================== --- /dev/null +++ lld/test/ELF/fatlto/fatlto.test @@ -0,0 +1,43 @@ +; REQUIRES: x86 + +; Following FatLTO and regular object files are produced as follows: +; clang -c -ffat-lto-objects -o a-fatLTO.o -x ir a-thinLTO.bc +; clang -c -ffat-lto-objects -o main-fatLTO.o -x ir main-thinLTO.bc +; clang -c -o a.c main.c + +; Basic FatLTO tests. +; Check that if input files contain .llvm.lto section +; RUN: yaml2obj %p/Inputs/a-fatLTO.yaml > %ta-fatLTO.o +; RUN: yaml2obj %p/Inputs/main-fatLTO.yaml > %tmain-fatLTO.o +; RUN: llvm-readobj -S %ta-fatLTO.o | FileCheck --check-prefix=a %s +; a: Name: .llvm.lto +; RUN: llvm-readobj -S %tmain-fatLTO.o | FileCheck --check-prefix=main %s +; main: Name: .llvm.lto + +; Final executable should not have .llvm.lto section no matter what the target is +; RUN: ld.lld -o %tfoo-fatLTO %ta-fatLTO.o %tmain-fatLTO.o -fat-lto-objects +; RUN: llvm-readobj -S %tfoo-fatLTO | FileCheck --check-prefix=lto-target %s +; Check if .llvm.lto section gets aggregated in LTO target +; lto-target-NOT: Name: .llvm.lto + +; Final executable should not have .llvm.lto section no matter what the target is +; RUN: ld.lld -o %tfoo-fatNoLTO %ta-fatLTO.o %tmain-fatLTO.o +; RUN: llvm-readobj -S %tfoo-fatNoLTO | FileCheck --check-prefix=non-lto-target %s +; Check if .llvm.lto section gets aggregated in non-LTO target +; non-lto-target-NOT: Name: .llvm.lto + +; Check if the LTO target executable produced from FatLTO object file is identical to the one produced from ThinLTO bitcode files +; RUN: llvm-as %p/Inputs/a-thinLTO.ll -o %ta-thinLTO.bc +; RUN: llvm-as %p/Inputs/main-thinLTO.ll -o %tmain-thinLTO.bc +; RUN: ld.lld -o %tfoo-thinLTO %ta-thinLTO.bc %tmain-thinLTO.bc +; RUN: obj2yaml %tfoo-fatLTO > %tfoo-fatLTO.yaml +; RUN: obj2yaml %tfoo-thinLTO > %tfoo-thinLTO.yaml +; RUN: diff %tfoo-fatLTO.yaml %tfoo-thinLTO.yaml + +; Check if the no-LTO target executable produced from FatLTO object file is identical to the one produced from regular object files +; RUN: yaml2obj %p/Inputs/a.yaml > %ta.o +; RUN: yaml2obj %p/Inputs/main.yaml > %tmain.o +; RUN: ld.lld -o %tfoo-noLTO %ta.o %tmain.o +; RUN: obj2yaml %tfoo-fatNoLTO > %tfoo-fatNoLTO.yaml +; RUN: obj2yaml %tfoo-noLTO > %tfoo-noLTO.yaml +; RUN: diff %tfoo-fatNoLTO.yaml %tfoo-noLTO.yaml Index: lld/test/ELF/fatlto/Inputs/main.yaml =================================================================== --- /dev/null +++ lld/test/ELF/fatlto/Inputs/main.yaml @@ -0,0 +1,111 @@ +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_X86_64 + SectionHeaderStringTable: .strtab +Sections: + - Name: .text + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC, SHF_EXECINSTR ] + AddressAlign: 0x10 + Content: 554889E5488D3D00000000B000E8000000005DC36666662E0F1F840000000000554889E54883EC10C745FC00000000E8000000004883C4105DC3 + - Name: .rodata.str1.1 + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC, SHF_MERGE, SHF_STRINGS ] + AddressAlign: 0x1 + EntSize: 0x1 + Content: 48690A00 + - Name: .comment + Type: SHT_PROGBITS + Flags: [ SHF_MERGE, SHF_STRINGS ] + AddressAlign: 0x1 + EntSize: 0x1 + Content: 00636C616E672076657273696F6E2031352E302E30202868747470733A2F2F6769746875622E636F6D2F6C6C766D2F6C6C766D2D70726F6A6563742E67697420346633376233363431353365653435343962653062666164353433653432316161323137316364332900 + - Name: .note.GNU-stack + Type: SHT_PROGBITS + AddressAlign: 0x1 + - Name: .eh_frame + Type: SHT_X86_64_UNWIND + Flags: [ SHF_ALLOC ] + AddressAlign: 0x8 + Content: 1400000000000000017A5200017810011B0C0708900100001C0000001C000000000000001400000000410E108602430D064F0C07080000001C0000003C000000000000001A00000000410E108602430D06550C0708000000 + - Name: .rela.text + Type: SHT_RELA + Flags: [ SHF_INFO_LINK ] + Link: .symtab + AddressAlign: 0x8 + Info: .text + Relocations: + - Offset: 0x7 + Symbol: .L.str + Type: R_X86_64_PC32 + Addend: -4 + - Offset: 0xE + Symbol: printf + Type: R_X86_64_PLT32 + Addend: -4 + - Offset: 0x30 + Symbol: foo1 + Type: R_X86_64_PLT32 + Addend: -4 + - Name: .rela.eh_frame + Type: SHT_RELA + Flags: [ SHF_INFO_LINK ] + Link: .symtab + AddressAlign: 0x8 + Info: .eh_frame + Relocations: + - Offset: 0x20 + Symbol: .text + Type: R_X86_64_PC32 + - Offset: 0x40 + Symbol: .text + Type: R_X86_64_PC32 + Addend: 32 + - Name: .llvm_addrsig + Type: SHT_LLVM_ADDRSIG + Flags: [ SHF_EXCLUDE ] + Link: .symtab + AddressAlign: 0x1 + Symbols: [ printf, foo1 ] + - Type: SectionHeaderTable + Sections: + - Name: .strtab + - Name: .text + - Name: .rela.text + - Name: .rodata.str1.1 + - Name: .comment + - Name: .note.GNU-stack + - Name: .eh_frame + - Name: .rela.eh_frame + - Name: .llvm_addrsig + - Name: .symtab +Symbols: + - Name: main.c + Type: STT_FILE + Index: SHN_ABS + - Name: .text + Type: STT_SECTION + Section: .text + - Name: .L.str + Type: STT_OBJECT + Section: .rodata.str1.1 + Size: 0x4 + - Name: foo4 + Type: STT_FUNC + Section: .text + Binding: STB_GLOBAL + Size: 0x14 + - Name: printf + Binding: STB_GLOBAL + - Name: main + Type: STT_FUNC + Section: .text + Binding: STB_GLOBAL + Value: 0x20 + Size: 0x1A + - Name: foo1 + Binding: STB_GLOBAL +... Index: lld/test/ELF/fatlto/Inputs/main.c =================================================================== --- /dev/null +++ lld/test/ELF/fatlto/Inputs/main.c @@ -0,0 +1,10 @@ +#include <stdio.h> +#include "a.h" + +void foo4(void) { + printf("Hi\n"); +} + +int main() { + return foo1(); +} Index: lld/test/ELF/fatlto/Inputs/main-thinLTO.ll =================================================================== --- /dev/null +++ lld/test/ELF/fatlto/Inputs/main-thinLTO.ll @@ -0,0 +1,48 @@ +; ModuleID = 'main-thinLTO.bc' +source_filename = "main.c" +target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +@.str = private unnamed_addr constant [4 x i8] c"Hi\0A\00", align 1 + +; Function Attrs: noinline nounwind optnone uwtable +define dso_local void @foo4() #0 { +entry: + %call = call i32 (ptr, ...) @printf(ptr noundef @.str) + ret void +} + +declare i32 @printf(ptr noundef, ...) #1 + +; Function Attrs: noinline nounwind optnone uwtable +define dso_local i32 @main() #0 { +entry: + %retval = alloca i32, align 4 + store i32 0, ptr %retval, align 4 + %call = call i32 @foo1() + ret i32 %call +} + +declare i32 @foo1() #1 + +attributes #0 = { noinline nounwind optnone uwtable "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" } +attributes #1 = { "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" } + +!llvm.module.flags = !{!0, !1, !2, !3, !4, !5} +!llvm.ident = !{!6} + +!0 = !{i32 1, !"wchar_size", i32 4} +!1 = !{i32 7, !"PIC Level", i32 2} +!2 = !{i32 7, !"PIE Level", i32 2} +!3 = !{i32 7, !"uwtable", i32 2} +!4 = !{i32 7, !"frame-pointer", i32 2} +!5 = !{i32 1, !"EnableSplitLTOUnit", i32 0} +!6 = !{!"clang version 15.0.0 (https://github.com/llvm/llvm-project.git 4f37b364153ee4549be0bfad543e421aa2171cd3)"} + +^0 = module: (path: "main-thinLTO.bc", hash: (399318317, 860371084, 1833432419, 324604037, 2895701215)) +^1 = gv: (name: ".str", summaries: (variable: (module: ^0, flags: (linkage: private, visibility: default, notEligibleToImport: 0, live: 0, dsoLocal: 1, canAutoHide: 0), varFlags: (readonly: 1, writeonly: 0, constant: 1)))) ; guid = 4917006661136692784 +^2 = gv: (name: "printf") ; guid = 7383291119112528047 +^3 = gv: (name: "foo1") ; guid = 7682762345278052905 +^4 = gv: (name: "foo4", summaries: (function: (module: ^0, flags: (linkage: external, visibility: default, notEligibleToImport: 0, live: 0, dsoLocal: 1, canAutoHide: 0), insts: 2, funcFlags: (readNone: 0, readOnly: 0, noRecurse: 0, returnDoesNotAlias: 0, noInline: 1, alwaysInline: 0, noUnwind: 1, mayThrow: 0, hasUnknownCall: 0, mustBeUnreachable: 0), calls: ((callee: ^2)), refs: (^1)))) ; guid = 11564431941544006930 +^5 = gv: (name: "main", summaries: (function: (module: ^0, flags: (linkage: external, visibility: default, notEligibleToImport: 0, live: 0, dsoLocal: 1, canAutoHide: 0), insts: 4, funcFlags: (readNone: 0, readOnly: 0, noRecurse: 0, returnDoesNotAlias: 0, noInline: 1, alwaysInline: 0, noUnwind: 1, mayThrow: 0, hasUnknownCall: 0, mustBeUnreachable: 0), calls: ((callee: ^3))))) ; guid = 15822663052811949562 +^6 = blockcount: 2 Index: lld/test/ELF/fatlto/Inputs/main-fatLTO.yaml =================================================================== --- /dev/null +++ lld/test/ELF/fatlto/Inputs/main-fatLTO.yaml @@ -0,0 +1,110 @@ +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_X86_64 + SectionHeaderStringTable: .strtab +Sections: + - Name: .text + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC, SHF_EXECINSTR ] + AddressAlign: 0x10 + Content: 554889E5488D3D00000000B000E8000000005DC36666662E0F1F840000000000554889E54883EC10C745FC00000000E8000000004883C4105DC3 + - Name: .rodata.str1.1 + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC, SHF_MERGE, SHF_STRINGS ] + AddressAlign: 0x1 + EntSize: 0x1 + Content: 48690A00 + - Name: .llvm.lto + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC ] + AddressAlign: 0x1 + Content: 4243C0DE3514000005000000620C30244D59BE669DFBB44F1BC8244401320500210C00005C0200000B02210002000000160000000781239141C80449061032399201840C250508191E048B628010450242920B42841032143808184B0A3242884870C421234412878C1041920264C808B1142043468820C901324284182A282A90317CB05C9120C4C8000000892000000E0000003222080920624600212B249810212524981019270C85A4906042645C2024648220A9390244096204C00433470006562830410D040000000051180000820000001B4823F8FFFFFFFF017000092883200C04C21CE4211CDAA11CDA001EDE211DDC811ECA411E807060077600887648077768037628877308077668037B288771A08777908736B8877420077A4007000E00C21DDEA10DE8411EC2011EE0211DDCE11CDAA01DC2811ED001A00779A88772008879A08770188775680378908777A0877218077A780779680371A80773308772908736988774D0877200F00020E8211CE4E11CCA811EDA601CE0A11E007CC0033B68033BA00380A0877090877328077A680373288770A0877A9087729807600DC6011FF0800DD6C01CF0611EE4810DD6A01DDA011FD8600DE6611ECA810DD6601EE6A11CE4800DD6001FF0E00E00821EEAC11DCAA10DC6011EEA01388772708772908774180760435808FFFFFFFF3F10E6200FE1D00EE5D006F0F00EE9E00EF4500EF20084033BB00340B8C33BB4013DC84338C0033CA4833B9C431BB44338D0033A00F4200FF5500E00310FF4100EE3B00E6D000FF2F00EF4500EE3400FEF200F6D200EF5600EE6500EF2D006F3900EFA500E001E00043D84833C9C4339D0431B8C033CD403800F7860076D6007740010F4100EF2700EE5400F6D600EE5100EF4500FF2500EF300ACC138E0031EB0C11A98033ECC833CB0C11AB4433BE0031BACC13CCC4339B0C11ACCC33C94831CB0C11AE0031EDC0140D0433DB84339B4C138C0433D00E7500EEE500EF2900EE3006C20860020050000491800000300000013826082200C13026142300013307CC0033BF8053BA08336A8077758077778877B70873660877470877AC08736380777A8870DA6500E6DD00E7A500E6D000F72700770A0077320077A300772D006F020077710077A300772A0077320076D000F72700772A0077640077A600774D006E9600774A0077640076D600E7800077A10077280076DE00E78A0077160077A300772A0077640076D300B71200778A0F48010211164C8489110402384610D06401A5FD8310184302C4900004000000010100030A4220802008000000000000000000280008654886100012000000000000000000010C090CA210E00000200000000000000080002185249C40104C00000000000000000004000121B040A6B0900006481000008000000321E981019114C908C092647C60443AA1228847228821100121D906905000000B1180000A10000003308801CC4E11C6614013D88433884C38C4280077978077398710CE6000FED100EF4800E330C421EC2C11DCEA11C6630053D88433884831BCC033DC8433D8C033DCC788C7470077B08077948877070077A700376788770208719CC110EEC900EE1300F6E300FE3F00EF0500E3310C41DDE211CD8211DC2611E6630893BBC833BD04339B4033CBC833C84033BCCF0147660077B680737688772680737808770908770600776280776F8057678877780875F08877118877298877998812CEEF00EEEE00EF5C00EEC300362C8A11CE4A11CCCA11CE4A11CDC611CCA211CC4811DCA6106D6904339C84339984339C84339B8C33894433888033B94C32FBC833CFC823BD4033BB0C30CC7698770588772708374680778608774188774A08719CE530FEE000FF2500EE4900EE3400FE1200EEC500E3320281DDCC11EC2411ED2211CDC811EDCE01CE4E11DEA011E66185138B0433A9C833BCC50247660077B68073760877778077898514CF4900FF0500E331E6A1ECA611CE8211DDEC11D7E011EE4A11CCC211DF0610654858338CCC33BB0433DD04339FCC23CE4433B88C33BB0C38CC50A877998877718877408077A28077298815CE3100EECC00EE5500EF33023C1D2411EE4E117D8E11DDE011E6648193BB0833DB4831B84C3388C4339CCC33CB8C139C8C33BD4033CCC48B471080776600771088771588719DBC60EEC600FEDE006F0200FE5300FE5200FF6500E6E100EE3300EE5300FF3E006E9E00EE4500EF83023E2EC611CC2811DD8E117EC211DE6211DC4211DD8211DE8211F66209D3BBC433DB80339948339CC58BC7070077778077A08077A488777708719CBE70EEF300FE1E00EE9400FE9A00FE530C3010373A8077718875F988770708774A08774D087720000792000004D000000721E482043880C19097232482023818C9191D144A01028643C3132428E9021A3782072014A921C8D840E000077636861725F73697A65504943204C6576656C504945204C6576656C75777461626C656672616D652D706F696E746572456E61626C6553706C69744C544F556E6974636C616E672076657273696F6E2031352E302E30202868747470733A2F2F6769746875622E636F6D2F6C6C766D2F6C6C766D2D70726F6A6563742E6769742034663337623336343135336565343534396265306266616435343365343231616132313731636433290000230845318250182308C53182502023084532C38004C90C83222C330CCAB0CC3028C432C3A014CB0C0362303304878C042628233636BB3697B637B23AB6321733B6B0B3B9510CA6711E284A858DCDAECD258DACCC8D6E944002000000A9180000250000000B0A7228877780077A587098433DB8C338B04339D0C382E61CC6A10DE8411EC2C11DE6211DE8211DDEC11D1634E3600EE7500FE1200FE4400FE1200FE7500EF4B08081077928877060077678877108077A28077258709CC338B4013BA4833D94C3026B1CD8211CDCE11CDC201CE4611CDC201CE8811EC2611CD0A11CC8611CC2811DD861C1010FF4200FE1500FF4800E00000000D11000000600000007CC3CA4833B9C033B94033DA0833C94433890C30100000061200000090000001304C188414180201818C9A2E140000003000000075010CD1461B64008C0B20000000000612000000E0000001304C14C445114A3306C4004C3008C1810000882C1816C100E04000005000000075010CD1461C64008C0B2D882111153052C000000000000A120000015000000A304C9500120AA0021328488102142C408891A40880C21224488103142520E10226384C4BC204486881112F280101942525A102264842A0014045023000400811A000502D4008080C000502128182C10000000007120000003000000320E10228401A80438304B008B8A363D757E614917A6B371ED5FDA0E4A9294EBE437F338B5050000650C000037000000120394B0010000000300000016000000320000004C0000000100000058000000000000005800000005000000D000000000000000480000001800000060000000060000000400000000000000D00000000000000000000000050000000000000004000000040000000400000004000000FFFFFFFF0024000008000000060000000800000006000000FFFFFFFF082400000E000000040000000E00000004000000FFFFFFFF0024000012000000040000001200000004000000FFFFFFFF0824000066000000060000000000000004000000FFFFFFFF00180000000000005D0C00001E000000120394EC000000002E737472666F6F347072696E74666D61696E666F6F3131352E302E3067697420346633376233363431353365653435343962653062666164353433653432316161323137316364337838365F36342D756E6B6E6F776E2D6C696E75782D676E756D61696E2E632E4C2E73747200000000 + - Name: .comment + Type: SHT_PROGBITS + Flags: [ SHF_MERGE, SHF_STRINGS ] + AddressAlign: 0x1 + EntSize: 0x1 + Content: 00636C616E672076657273696F6E2031352E302E30202868747470733A2F2F6769746875622E636F6D2F6C6C766D2F6C6C766D2D70726F6A6563742E67697420346633376233363431353365653435343962653062666164353433653432316161323137316364332900 + - Name: .note.GNU-stack + Type: SHT_PROGBITS + AddressAlign: 0x1 + - Name: .eh_frame + Type: SHT_X86_64_UNWIND + Flags: [ SHF_ALLOC ] + AddressAlign: 0x8 + Content: 1400000000000000017A5200017810011B0C0708900100001C0000001C000000000000001400000000410E108602430D064F0C07080000001C0000003C000000000000001A00000000410E108602430D06550C0708000000 + - Name: .rela.text + Type: SHT_RELA + Flags: [ SHF_INFO_LINK ] + Link: .symtab + AddressAlign: 0x8 + Info: .text + Relocations: + - Offset: 0x7 + Symbol: .L.str + Type: R_X86_64_PC32 + Addend: -4 + - Offset: 0xE + Symbol: printf + Type: R_X86_64_PLT32 + Addend: -4 + - Offset: 0x30 + Symbol: foo1 + Type: R_X86_64_PLT32 + Addend: -4 + - Name: .rela.eh_frame + Type: SHT_RELA + Flags: [ SHF_INFO_LINK ] + Link: .symtab + AddressAlign: 0x8 + Info: .eh_frame + Relocations: + - Offset: 0x20 + Symbol: .text + Type: R_X86_64_PC32 + - Offset: 0x40 + Symbol: .text + Type: R_X86_64_PC32 + Addend: 32 + - Type: SectionHeaderTable + Sections: + - Name: .strtab + - Name: .text + - Name: .rela.text + - Name: .rodata.str1.1 + - Name: .llvm.lto + - Name: .comment + - Name: .note.GNU-stack + - Name: .eh_frame + - Name: .rela.eh_frame + - Name: .symtab +Symbols: + - Name: main.c + Type: STT_FILE + Index: SHN_ABS + - Name: .text + Type: STT_SECTION + Section: .text + - Name: .L.str + Type: STT_OBJECT + Section: .rodata.str1.1 + Size: 0x4 + - Name: foo4 + Type: STT_FUNC + Section: .text + Binding: STB_GLOBAL + Size: 0x14 + - Name: printf + Binding: STB_GLOBAL + - Name: main + Type: STT_FUNC + Section: .text + Binding: STB_GLOBAL + Value: 0x20 + Size: 0x1A + - Name: foo1 + Binding: STB_GLOBAL +... Index: lld/test/ELF/fatlto/Inputs/a.yaml =================================================================== --- /dev/null +++ lld/test/ELF/fatlto/Inputs/a.yaml @@ -0,0 +1,120 @@ +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_X86_64 + SectionHeaderStringTable: .strtab +Sections: + - Name: .text + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC, SHF_EXECINSTR ] + AddressAlign: 0x10 + Content: 554889E5C70500000000FFFFFFFF5DC3554889E54883EC10C745FC00000000833D00000000000F8D08000000E81F0000008945FC8B45FC83C02A8945FC8B45FC4883C4105DC3662E0F1F840000000000554889E5E800000000B80A0000005DC3 + - Name: .bss + Type: SHT_NOBITS + Flags: [ SHF_WRITE, SHF_ALLOC ] + AddressAlign: 0x4 + Size: 0x4 + - Name: .comment + Type: SHT_PROGBITS + Flags: [ SHF_MERGE, SHF_STRINGS ] + AddressAlign: 0x1 + EntSize: 0x1 + Content: 00636C616E672076657273696F6E2031352E302E30202868747470733A2F2F6769746875622E636F6D2F6C6C766D2F6C6C766D2D70726F6A6563742E67697420346633376233363431353365653435343962653062666164353433653432316161323137316364332900 + - Name: .note.GNU-stack + Type: SHT_PROGBITS + AddressAlign: 0x1 + - Name: .eh_frame + Type: SHT_X86_64_UNWIND + Flags: [ SHF_ALLOC ] + AddressAlign: 0x8 + Content: 1400000000000000017A5200017810011B0C0708900100001C0000001C000000000000001000000000410E108602430D064B0C07080000001C0000003C000000000000003600000000410E108602430D06710C07080000001C0000005C000000000000001000000000410E108602430D064B0C0708000000 + - Name: .rela.text + Type: SHT_RELA + Flags: [ SHF_INFO_LINK ] + Link: .symtab + AddressAlign: 0x8 + Info: .text + Relocations: + - Offset: 0x6 + Symbol: .bss + Type: R_X86_64_PC32 + Addend: -8 + - Offset: 0x21 + Symbol: .bss + Type: R_X86_64_PC32 + Addend: -5 + - Offset: 0x55 + Symbol: foo4 + Type: R_X86_64_PLT32 + Addend: -4 + - Name: .rela.eh_frame + Type: SHT_RELA + Flags: [ SHF_INFO_LINK ] + Link: .symtab + AddressAlign: 0x8 + Info: .eh_frame + Relocations: + - Offset: 0x20 + Symbol: .text + Type: R_X86_64_PC32 + - Offset: 0x40 + Symbol: .text + Type: R_X86_64_PC32 + Addend: 16 + - Offset: 0x60 + Symbol: .text + Type: R_X86_64_PC32 + Addend: 80 + - Name: .llvm_addrsig + Type: SHT_LLVM_ADDRSIG + Flags: [ SHF_EXCLUDE ] + Link: .symtab + AddressAlign: 0x1 + Symbols: [ foo3, foo4, i ] + - Type: SectionHeaderTable + Sections: + - Name: .strtab + - Name: .text + - Name: .rela.text + - Name: .bss + - Name: .comment + - Name: .note.GNU-stack + - Name: .eh_frame + - Name: .rela.eh_frame + - Name: .llvm_addrsig + - Name: .symtab +Symbols: + - Name: a.c + Type: STT_FILE + Index: SHN_ABS + - Name: .text + Type: STT_SECTION + Section: .text + - Name: i + Type: STT_OBJECT + Section: .bss + Size: 0x4 + - Name: foo3 + Type: STT_FUNC + Section: .text + Value: 0x50 + Size: 0x10 + - Name: .bss + Type: STT_SECTION + Section: .bss + - Name: foo2 + Type: STT_FUNC + Section: .text + Binding: STB_GLOBAL + Size: 0x10 + - Name: foo1 + Type: STT_FUNC + Section: .text + Binding: STB_GLOBAL + Value: 0x10 + Size: 0x36 + - Name: foo4 + Binding: STB_GLOBAL +... Index: lld/test/ELF/fatlto/Inputs/a.h =================================================================== --- /dev/null +++ lld/test/ELF/fatlto/Inputs/a.h @@ -0,0 +1,3 @@ +extern int foo1(void); +extern void foo2(void); +extern void foo4(void); Index: lld/test/ELF/fatlto/Inputs/a.c =================================================================== --- /dev/null +++ lld/test/ELF/fatlto/Inputs/a.c @@ -0,0 +1,24 @@ +#include "a.h" + +static signed int i = 0; + +void foo2(void) { + i = -1; +} + +static int foo3() { + foo4(); + return 10; +} + +int foo1(void) { + int data = 0; + + if (i < 0) + data = foo3(); + + data = data + 42; + return data; +} + + Index: lld/test/ELF/fatlto/Inputs/a-thinLTO.ll =================================================================== --- /dev/null +++ lld/test/ELF/fatlto/Inputs/a-thinLTO.ll @@ -0,0 +1,66 @@ +; ModuleID = 'a-thinLTO.bc' +source_filename = "a.c" +target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +@i = internal global i32 0, align 4 + +; Function Attrs: noinline nounwind optnone uwtable +define dso_local void @foo2() #0 { +entry: + store i32 -1, ptr @i, align 4 + ret void +} + +; Function Attrs: noinline nounwind optnone uwtable +define dso_local i32 @foo1() #0 { +entry: + %data = alloca i32, align 4 + store i32 0, ptr %data, align 4 + %0 = load i32, ptr @i, align 4 + %cmp = icmp slt i32 %0, 0 + br i1 %cmp, label %if.then, label %if.end + +if.then: ; preds = %entry + %call = call i32 @foo3() + store i32 %call, ptr %data, align 4 + br label %if.end + +if.end: ; preds = %if.then, %entry + %1 = load i32, ptr %data, align 4 + %add = add nsw i32 %1, 42 + store i32 %add, ptr %data, align 4 + %2 = load i32, ptr %data, align 4 + ret i32 %2 +} + +; Function Attrs: noinline nounwind optnone uwtable +define internal i32 @foo3() #0 { +entry: + call void @foo4() + ret i32 10 +} + +declare void @foo4() #1 + +attributes #0 = { noinline nounwind optnone uwtable "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" } +attributes #1 = { "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" } + +!llvm.module.flags = !{!0, !1, !2, !3, !4, !5} +!llvm.ident = !{!6} + +!0 = !{i32 1, !"wchar_size", i32 4} +!1 = !{i32 7, !"PIC Level", i32 2} +!2 = !{i32 7, !"PIE Level", i32 2} +!3 = !{i32 7, !"uwtable", i32 2} +!4 = !{i32 7, !"frame-pointer", i32 2} +!5 = !{i32 1, !"EnableSplitLTOUnit", i32 0} +!6 = !{!"clang version 15.0.0 (https://github.com/llvm/llvm-project.git 4f37b364153ee4549be0bfad543e421aa2171cd3)"} + +^0 = module: (path: "a-thinLTO.bc", hash: (3275101976, 447782533, 3223852026, 2590893220, 1438244087)) +^1 = gv: (name: "foo2", summaries: (function: (module: ^0, flags: (linkage: external, visibility: default, notEligibleToImport: 0, live: 0, dsoLocal: 1, canAutoHide: 0), insts: 2, funcFlags: (readNone: 0, readOnly: 0, noRecurse: 0, returnDoesNotAlias: 0, noInline: 1, alwaysInline: 0, noUnwind: 1, mayThrow: 0, hasUnknownCall: 0, mustBeUnreachable: 0), refs: (writeonly ^2)))) ; guid = 2494702099028631698 +^2 = gv: (name: "i", summaries: (variable: (module: ^0, flags: (linkage: internal, visibility: default, notEligibleToImport: 0, live: 0, dsoLocal: 1, canAutoHide: 0), varFlags: (readonly: 1, writeonly: 1, constant: 0)))) ; guid = 2708120569957007488 +^3 = gv: (name: "foo1", summaries: (function: (module: ^0, flags: (linkage: external, visibility: default, notEligibleToImport: 0, live: 0, dsoLocal: 1, canAutoHide: 0), insts: 13, funcFlags: (readNone: 0, readOnly: 0, noRecurse: 0, returnDoesNotAlias: 0, noInline: 1, alwaysInline: 0, noUnwind: 1, mayThrow: 0, hasUnknownCall: 0, mustBeUnreachable: 0), calls: ((callee: ^5)), refs: (readonly ^2)))) ; guid = 7682762345278052905 +^4 = gv: (name: "foo4") ; guid = 11564431941544006930 +^5 = gv: (name: "foo3", summaries: (function: (module: ^0, flags: (linkage: internal, visibility: default, notEligibleToImport: 0, live: 0, dsoLocal: 1, canAutoHide: 0), insts: 2, funcFlags: (readNone: 0, readOnly: 0, noRecurse: 0, returnDoesNotAlias: 0, noInline: 1, alwaysInline: 0, noUnwind: 1, mayThrow: 0, hasUnknownCall: 0, mustBeUnreachable: 0), calls: ((callee: ^4))))) ; guid = 17367728344439303071 +^6 = blockcount: 5 Index: lld/test/ELF/fatlto/Inputs/a-fatLTO.yaml =================================================================== --- /dev/null +++ lld/test/ELF/fatlto/Inputs/a-fatLTO.yaml @@ -0,0 +1,119 @@ +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_X86_64 + SectionHeaderStringTable: .strtab +Sections: + - Name: .text + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC, SHF_EXECINSTR ] + AddressAlign: 0x10 + Content: 554889E5C70500000000FFFFFFFF5DC3554889E54883EC10C745FC00000000833D00000000000F8D08000000E81F0000008945FC8B45FC83C02A8945FC8B45FC4883C4105DC3662E0F1F840000000000554889E5E800000000B80A0000005DC3 + - Name: .bss + Type: SHT_NOBITS + Flags: [ SHF_WRITE, SHF_ALLOC ] + AddressAlign: 0x4 + Size: 0x4 + - Name: .llvm.lto + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC ] + AddressAlign: 0x1 + Content: 4243C0DE3514000005000000620C30244D59BE669DFBB44F1BC8244401320500210C00007B0200000B02210002000000160000000781239141C80449061032399201840C250508191E048B628010450242920B42841032143808184B0A3242884870C421234412878C1041920264C808B1142043468820C901324284182A282A90317CB05C9120C4C8000000892000000D0000003222080920624600212B249810212524981019270C85A4906042645C2024648200A93902301801308198200602E608822900000051180000810000001B4823F8FFFFFFFF017000092883200C04C21CE4211CDAA11CDA001EDE211DDC811ECA411E807060077600887648077768037628877308077668037B288771A08777908736B8877420077A4007000E00C21DDEA10DE8411EC2011EE0211DDCE11CDAA01DC2811ED001A00779A88772008879A08770188775680378908777A0877218077A780779680371A80773308772908736988774D0877200F00020E8211CE4E11CCA811EDA601CE0A11E007CC0033B68033BA00380A0877090877328077A680373288770A0877A9087729807600DC6011FF0800DD6C01CF0611EE4810DD6A01DDA011FD8600DE6611ECA810DD6601EE6A11CE4800DD6001FF0E00E00821EEAC11DCAA10DC6011EEA01388772708772908774180760435808FFFFFFFF3F10E6200FE1D00EE5D006F0F00EE9E00EF4500EF20084033BB00340B8C33BB4013DC84338C0033CA4833B9C431BB44338D0033A00F4200FF5500E00310FF4100EE3B00E6D000FF2F00EF4500EE3400FEF200F6D200EF5600EE6500EF2D006F3900EFA500E001E00043D84833C9C4339D0431B8C033CD403800F7860076D6007740010F4100EF2700EE5400F6D600EE5100EF4500FF2500EF300ACC138E0031EB0C11A98033ECC833CB0C11AB4433BE0031BACC13CCC4339B0C11ACCC33C94831CB0C11AE0031EDC0140D0433DB84339B4C138C0433D00E7500EEE500EF2900EE300000000004918000002000000138260422000000013307CC0033BF8053BA08336A8077758077778877B70873660877470877AC08736380777A8870DA6500E6DD00E7A500E6D000F72700770A0077320077A300772D006F020077710077A300772A0077320076D000F72700772A0077640077A600774D006E9600774A0077640076D600E7800077A10077280076DE00E78A0077160077A300772A0077640076D300B71200778A0F48010210964C84811114023846107C017764C401008C6300000000000000401000CA902620000200000000000000080000880215541100000040000000000000010000130A44A08020086000000000000000002200086540D310001200000000000000000000480C40681C278020000592007000000321E981019114C908C092647C604438A118012288472280200000000B1180000A10000003308801CC4E11C6614013D88433884C38C4280077978077398710CE6000FED100EF4800E330C421EC2C11DCEA11C6630053D88433884831BCC033DC8433D8C033DCC788C7470077B08077948877070077A700376788770208719CC110EEC900EE1300F6E300FE3F00EF0500E3310C41DDE211CD8211DC2611E6630893BBC833BD04339B4033CBC833C84033BCCF0147660077B680737688772680737808770908770600776280776F8057678877780875F08877118877298877998812CEEF00EEEE00EF5C00EEC300362C8A11CE4A11CCCA11CE4A11CDC611CCA211CC4811DCA6106D6904339C84339984339C84339B8C33894433888033B94C32FBC833CFC823BD4033BB0C30CC7698770588772708374680778608774188774A08719CE530FEE000FF2500EE4900EE3400FE1200EEC500E3320281DDCC11EC2411ED2211CDC811EDCE01CE4E11DEA011E66185138B0433A9C833BCC50247660077B68073760877778077898514CF4900FF0500E331E6A1ECA611CE8211DDEC11D7E011EE4A11CCC211DF0610654858338CCC33BB0433DD04339FCC23CE4433B88C33BB0C38CC50A877998877718877408077A28077298815CE3100EECC00EE5500EF33023C1D2411EE4E117D8E11DDE011E6648193BB0833DB4831B84C3388C4339CCC33CB8C139C8C33BD4033CCC48B471080776600771088771588719DBC60EEC600FEDE006F0200FE5300FE5200FF6500E6E100EE3300EE5300FF3E006E9E00EE4500EF83023E2EC611CC2811DD8E117EC211DE6211DC4211DD8211DE8211F66209D3BBC433DB80339948339CC58BC7070077778077A08077A488777708719CBE70EEF300FE1E00EE9400FE9A00FE530C3010373A8077718875F988770708774A08774D087720000792000004D000000721E482043880C19097232482023818C9191D144A01028643C3132428E9021A3782072014A921C8D840E000077636861725F73697A65504943204C6576656C504945204C6576656C75777461626C656672616D652D706F696E746572456E61626C6553706C69744C544F556E6974636C616E672076657273696F6E2031352E302E30202868747470733A2F2F6769746875622E636F6D2F6C6C766D2F6C6C766D2D70726F6A6563742E67697420346633376233363431353365653435343962653062666164353433653432316161323137316364332900002308813182101C2308013282102423084131C38004C90C83222C330CCAB0CC3028C432C3A014CB0C0362303304878C042628233636BB3697B637B23AB6321733B6B0B3B9510CA6711E284A858DCDAECD258DACCC8D6E944002000000A9180000250000000B0A7228877780077A587098433DB8C338B04339D0C382E61CC6A10DE8411EC2C11DE6211DE8211DDEC11D1634E3600EE7500FE1200FE4400FE1200FE7500EF4B08081077928877060077678877108077A28077258709CC338B4013BA4833D94C3026B1CD8211CDCE11CDC201CE4611CDC201CE8811EC2611CD0A11CC8611CC2811DD861C1010FF4200FE1500FF4800E00000000D11000000600000007CC3CA4833B9C033B94033DA0833C94433890C301000000612000000B0000001304412C100000000100000014350000C306C4120C8086030100000002000000075010CD146100000000000061200000200000001304432C100000000100000014450500331141608CC2B001111C03404C8CE18600418359864008460C080004C18058860D082218805902818818181820306C4018C1009011638370200000000B000000D63008CC730005D14C1176020652F8443318C4000C832D10033001177020853F1D44630E84002C0B0000000000000000612000000B0000001304412C100000000100000014450100230604008260301C1B84030102000000075010CD1461000000000000A120000017000000A304C9500120AA0021328488102142C408891A40880C21224488103142520E10226384C4BC204486881112F280101942525A102264842A001404502320028012008506D488000160A0064E4100350020203080530806060B0A0000007120000004000000320E10228401A30428004B8003D204008B0A1C2FD9391C4AFA0CD746FF49580407B2CC77B315EEF39E773500650C000037000000120394B0010000000300000011000000320000004C0000000100000058000000000000005800000005000000D00000000000000043000000180000005B000000030000000100000000000000D00000000000000000000000050000000000000001000000040000000100000004000000FFFFFFFF0024000005000000040000000500000004000000FFFFFFFF0024000009000000040000000900000004000000FFFFFFFF002000000D000000040000000D00000004000000FFFFFFFF0824000000000000010000000000000001000000FFFFFFFF00000000000000005D0C00001B000000120394BE0000000069666F6F32666F6F31666F6F33666F6F3431352E302E3067697420346633376233363431353365653435343962653062666164353433653432316161323137316364337838365F36342D756E6B6E6F776E2D6C696E75782D676E75612E63000000000000 + - Name: .comment + Type: SHT_PROGBITS + Flags: [ SHF_MERGE, SHF_STRINGS ] + AddressAlign: 0x1 + EntSize: 0x1 + Content: 00636C616E672076657273696F6E2031352E302E30202868747470733A2F2F6769746875622E636F6D2F6C6C766D2F6C6C766D2D70726F6A6563742E67697420346633376233363431353365653435343962653062666164353433653432316161323137316364332900 + - Name: .note.GNU-stack + Type: SHT_PROGBITS + AddressAlign: 0x1 + - Name: .eh_frame + Type: SHT_X86_64_UNWIND + Flags: [ SHF_ALLOC ] + AddressAlign: 0x8 + Content: 1400000000000000017A5200017810011B0C0708900100001C0000001C000000000000001000000000410E108602430D064B0C07080000001C0000003C000000000000003600000000410E108602430D06710C07080000001C0000005C000000000000001000000000410E108602430D064B0C0708000000 + - Name: .rela.text + Type: SHT_RELA + Flags: [ SHF_INFO_LINK ] + Link: .symtab + AddressAlign: 0x8 + Info: .text + Relocations: + - Offset: 0x6 + Symbol: .bss + Type: R_X86_64_PC32 + Addend: -8 + - Offset: 0x21 + Symbol: .bss + Type: R_X86_64_PC32 + Addend: -5 + - Offset: 0x55 + Symbol: foo4 + Type: R_X86_64_PLT32 + Addend: -4 + - Name: .rela.eh_frame + Type: SHT_RELA + Flags: [ SHF_INFO_LINK ] + Link: .symtab + AddressAlign: 0x8 + Info: .eh_frame + Relocations: + - Offset: 0x20 + Symbol: .text + Type: R_X86_64_PC32 + - Offset: 0x40 + Symbol: .text + Type: R_X86_64_PC32 + Addend: 16 + - Offset: 0x60 + Symbol: .text + Type: R_X86_64_PC32 + Addend: 80 + - Type: SectionHeaderTable + Sections: + - Name: .strtab + - Name: .text + - Name: .rela.text + - Name: .bss + - Name: .llvm.lto + - Name: .comment + - Name: .note.GNU-stack + - Name: .eh_frame + - Name: .rela.eh_frame + - Name: .symtab +Symbols: + - Name: a.c + Type: STT_FILE + Index: SHN_ABS + - Name: .text + Type: STT_SECTION + Section: .text + - Name: i + Type: STT_OBJECT + Section: .bss + Size: 0x4 + - Name: foo3 + Type: STT_FUNC + Section: .text + Value: 0x50 + Size: 0x10 + - Name: .bss + Type: STT_SECTION + Section: .bss + - Name: foo2 + Type: STT_FUNC + Section: .text + Binding: STB_GLOBAL + Size: 0x10 + - Name: foo1 + Type: STT_FUNC + Section: .text + Binding: STB_GLOBAL + Value: 0x10 + Size: 0x36 + - Name: foo4 + Binding: STB_GLOBAL +... Index: lld/ELF/Options.td =================================================================== --- lld/ELF/Options.td +++ lld/ELF/Options.td @@ -608,6 +608,10 @@ def thinlto_single_module_eq: JJ<"thinlto-single-module=">, HelpText<"Specific a single module to compile in ThinLTO mode, for debugging only">; +defm fatlto_objects: B<"fat-lto-objects", + "Use the embedded bitcode in the .llvm.lto section of the object file", + "Use the assembly in the object file (default)">; + def: J<"plugin-opt=O">, Alias<lto_O>, HelpText<"Alias for --lto-O">; def: F<"plugin-opt=debug-pass-manager">, Alias<lto_debug_pass_manager>, HelpText<"Alias for --lto-debug-pass-manager">; Index: lld/ELF/InputFiles.cpp =================================================================== --- lld/ELF/InputFiles.cpp +++ lld/ELF/InputFiles.cpp @@ -1715,6 +1715,13 @@ if (isBitcode(mb)) return make<BitcodeFile>(mb, archiveName, offsetInArchive, /*lazy=*/false); + // If it is a fatLTO object file + if (config->fatLTOObjects) { + Expected<MemoryBufferRef> fatLTOData = IRObjectFile::findBitcodeInMemBuffer(mb); + if (!errorToBool(fatLTOData.takeError())) + return make<BitcodeFile>(*fatLTOData, archiveName, offsetInArchive, /*lazy=*/false); + } + switch (getELFKind(mb, archiveName)) { case ELF32LEKind: return make<ObjFile<ELF32LE>>(mb, archiveName); Index: lld/ELF/Driver.cpp =================================================================== --- lld/ELF/Driver.cpp +++ lld/ELF/Driver.cpp @@ -1023,6 +1023,11 @@ args.hasFlag(OPT_allow_multiple_definition, OPT_no_allow_multiple_definition, false) || hasZOption(args, "muldefs"); + + config->fatLTOObjects = + args.hasFlag(OPT_fatlto_objects, + OPT_no_fatlto_objects, false); + config->androidMemtagHeap = args.hasFlag(OPT_android_memtag_heap, OPT_no_android_memtag_heap, false); config->androidMemtagStack = args.hasFlag(OPT_android_memtag_stack, @@ -2626,10 +2631,17 @@ // Now that we have a complete list of input files. // Beyond this point, no new files are added. // Aggregate all input sections into one place. - for (InputFile *f : ctx->objectFiles) - for (InputSectionBase *s : f->getSections()) - if (s && s != &InputSection::discarded) + for (InputFile *f : ctx->objectFiles) { + for (InputSectionBase *s : f->getSections()) { + if (!s) + continue; + // Do not aggregate the .llvm.lto section + if (s->name == ".llvm.lto") + continue; + if (s != &InputSection::discarded) inputSections.push_back(s); + } + } for (BinaryFile *f : ctx->binaryFiles) for (InputSectionBase *s : f->getSections()) inputSections.push_back(cast<InputSection>(s)); Index: lld/ELF/Config.h =================================================================== --- lld/ELF/Config.h +++ lld/ELF/Config.h @@ -155,6 +155,7 @@ uint64_t> callGraphProfile; bool allowMultipleDefinition; + bool fatLTOObjects; bool androidPackDynRelocs = false; bool armHasBlx = false; bool armHasMovtMovw = false; Index: clang/lib/Driver/ToolChains/Clang.cpp =================================================================== --- clang/lib/Driver/ToolChains/Clang.cpp +++ clang/lib/Driver/ToolChains/Clang.cpp @@ -7117,6 +7117,9 @@ } } + if (Args.hasArg(options::OPT_ffat_lto_objects)) + CmdArgs.push_back("-ffat-lto-objects"); + if (Args.hasArg(options::OPT_forder_file_instrumentation)) { CmdArgs.push_back("-forder-file-instrumentation"); // Enable order file instrumentation when ThinLTO is not on. When ThinLTO is Index: clang/lib/Driver/Driver.cpp =================================================================== --- clang/lib/Driver/Driver.cpp +++ clang/lib/Driver/Driver.cpp @@ -1314,6 +1314,11 @@ BitcodeEmbed = static_cast<BitcodeEmbedMode>(Model); } + // Process -ffat-lto-objects flag. + if (Arg *A = Args.getLastArg(options::OPT_ffat_lto_objects)) { + BitcodeEmbed = EmbedBitcode; + } + // Remove existing compilation database so that each job can append to it. if (Arg *A = Args.getLastArg(options::OPT_MJ)) llvm::sys::fs::remove(A->getValue()); Index: clang/lib/CodeGen/CodeGenAction.cpp =================================================================== --- clang/lib/CodeGen/CodeGenAction.cpp +++ clang/lib/CodeGen/CodeGenAction.cpp @@ -375,6 +375,7 @@ } EmbedBitcode(getModule(), CodeGenOpts, llvm::MemoryBufferRef()); + EmbedFatLTO(getModule(), CodeGenOpts, llvm::MemoryBufferRef()); EmitBackendOutput(Diags, HeaderSearchOpts, CodeGenOpts, TargetOpts, LangOpts, C.getTargetInfo().getDataLayoutString(), @@ -1172,6 +1173,8 @@ EmbedObject(TheModule.get(), CodeGenOpts, Diagnostics); EmbedBitcode(TheModule.get(), CodeGenOpts, *MainFile); + EmbedFatLTO(TheModule.get(), CodeGenOpts, *MainFile); + LLVMContext &Ctx = TheModule->getContext(); Index: clang/lib/CodeGen/BackendUtil.cpp =================================================================== --- clang/lib/CodeGen/BackendUtil.cpp +++ clang/lib/CodeGen/BackendUtil.cpp @@ -1197,6 +1197,8 @@ // __LLVM,__bitcode section. void clang::EmbedBitcode(llvm::Module *M, const CodeGenOptions &CGOpts, llvm::MemoryBufferRef Buf) { + if (CGOpts.FatLTO) + return; if (CGOpts.getEmbedBitcode() == CodeGenOptions::Embed_Off) return; llvm::embedBitcodeInModule( @@ -1205,6 +1207,15 @@ CGOpts.CmdArgs); } +// With -ffat-lto-objects, save a copy of the llvm IR as data in the +// .llvm.lto section. +void clang::EmbedFatLTO(llvm::Module *M, const CodeGenOptions &CGOpts, + llvm::MemoryBufferRef Buf) { + if (!CGOpts.FatLTO) + return; + llvm::embedBitcodeInFatObject(*M, Buf); +} + void clang::EmbedObject(llvm::Module *M, const CodeGenOptions &CGOpts, DiagnosticsEngine &Diags) { if (CGOpts.OffloadObjects.empty()) Index: clang/include/clang/Driver/ToolChain.h =================================================================== --- clang/include/clang/Driver/ToolChain.h +++ clang/include/clang/Driver/ToolChain.h @@ -569,6 +569,9 @@ /// SupportsEmbeddedBitcode - Does this tool chain support embedded bitcode. virtual bool SupportsEmbeddedBitcode() const { return false; } + /// SupportsFatLTO - Does this toolchain support fat LTO objects. + virtual bool SupportsFatLTO() const { return true; } + /// getThreadModel() - Which thread model does this target use? virtual std::string getThreadModel() const { return "posix"; } Index: clang/include/clang/Driver/Options.td =================================================================== --- clang/include/clang/Driver/Options.td +++ clang/include/clang/Driver/Options.td @@ -2124,6 +2124,11 @@ Alias<flto_EQ>, AliasArgs<["full"]>, HelpText<"Enable LTO in 'full' mode">; def flto : Flag<["-"], "flto">, Flags<[CoreOption, CC1Option]>, Group<f_Group>, Alias<flto_EQ>, AliasArgs<["full"]>, HelpText<"Enable LTO in 'full' mode">; +defm fat_lto_objects : BoolFOption<"fat-lto-objects", + CodeGenOpts<"FatLTO">, DefaultFalse, + PosFlag<SetTrue, [], "Enable fat LTO object support">, + NegFlag<SetFalse, [], "Disable fat LTO object support">, + BothFlags<[CC1Option], "">>; def fno_lto : Flag<["-"], "fno-lto">, Flags<[CoreOption, CC1Option]>, Group<f_Group>, HelpText<"Disable LTO mode (default)">; def foffload_lto_EQ : Joined<["-"], "foffload-lto=">, Flags<[CoreOption]>, Group<f_Group>, @@ -4669,7 +4674,6 @@ defm reorder_blocks : BooleanFFlag<"reorder-blocks">, Group<clang_ignored_gcc_optimization_f_Group>; defm branch_count_reg : BooleanFFlag<"branch-count-reg">, Group<clang_ignored_gcc_optimization_f_Group>; defm default_inline : BooleanFFlag<"default-inline">, Group<clang_ignored_gcc_optimization_f_Group>; -defm fat_lto_objects : BooleanFFlag<"fat-lto-objects">, Group<clang_ignored_gcc_optimization_f_Group>; defm float_store : BooleanFFlag<"float-store">, Group<clang_ignored_gcc_optimization_f_Group>; defm friend_injection : BooleanFFlag<"friend-injection">, Group<clang_ignored_f_Group>; defm function_attribute_list : BooleanFFlag<"function-attribute-list">, Group<clang_ignored_f_Group>; Index: clang/include/clang/CodeGen/BackendUtil.h =================================================================== --- clang/include/clang/CodeGen/BackendUtil.h +++ clang/include/clang/CodeGen/BackendUtil.h @@ -45,6 +45,9 @@ void EmbedBitcode(llvm::Module *M, const CodeGenOptions &CGOpts, llvm::MemoryBufferRef Buf); + void EmbedFatLTO(llvm::Module *M, const CodeGenOptions &CGOpts, + llvm::MemoryBufferRef Buf); + void EmbedObject(llvm::Module *M, const CodeGenOptions &CGOpts, DiagnosticsEngine &Diags); } Index: clang/include/clang/Basic/CodeGenOptions.def =================================================================== --- clang/include/clang/Basic/CodeGenOptions.def +++ clang/include/clang/Basic/CodeGenOptions.def @@ -161,6 +161,7 @@ ///< compile step. CODEGENOPT(LTOUnit, 1, 0) ///< Emit IR to support LTO unit features (CFI, whole ///< program vtable opt). +CODEGENOPT(FatLTO, 1, 0) ///< Set when -ffat-lto-objects is enabled. CODEGENOPT(EnableSplitLTOUnit, 1, 0) ///< Enable LTO unit splitting to support /// CFI and traditional whole program /// devirtualization that require whole
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits