llvm.org GIT mirror llvm / 9283a09
IR: Replace the "Linker Options" module flag with "llvm.linker.options" named metadata. The new metadata is easier to manipulate than module flags. Differential Revision: https://reviews.llvm.org/D31349 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@305227 91177308-0d34-0410-b5e6-96231b3b80d8 Peter Collingbourne 3 years ago
18 changed file(s) with 126 addition(s) and 127 deletion(s). Raw diff Collapse all Expand all
53515351 - A module with ``Objective-C Garbage Collection`` set to 0 cannot be
53525352 merged with a module with ``Objective-C GC Only`` set to 6.
53535353
5354 Automatic Linker Flags Module Flags Metadata
5355 --------------------------------------------
5354 C type width Module Flags Metadata
5355 ----------------------------------
5356
5357 The ARM backend emits a section into each generated object file describing the
5358 options that it was compiled with (in a compiler-independent way) to prevent
5359 linking incompatible objects, and to allow automatic library selection. Some
5360 of these options are not visible at the IR level, namely wchar_t width and enum
5361 width.
5362
5363 To pass this information to the backend, these options are encoded in module
5364 flags metadata, using the following key-value pairs:
5365
5366 .. list-table::
5367 :header-rows: 1
5368 :widths: 30 70
5369
5370 * - Key
5371 - Value
5372
5373 * - short_wchar
5374 - * 0 --- sizeof(wchar_t) == 4
5375 * 1 --- sizeof(wchar_t) == 2
5376
5377 * - short_enum
5378 - * 0 --- Enums are at least as large as an ``int``.
5379 * 1 --- Enums are stored in the smallest integer type which can
5380 represent all of its values.
5381
5382 For example, the following metadata section specifies that the module was
5383 compiled with a ``wchar_t`` width of 4 bytes, and the underlying type of an
5384 enum is the smallest type which can represent all of its values::
5385
5386 !llvm.module.flags = !{!0, !1}
5387 !0 = !{i32 1, !"short_wchar", i32 1}
5388 !1 = !{i32 1, !"short_enum", i32 0}
5389
5390 Automatic Linker Flags Named Metadata
5391 =====================================
53565392
53575393 Some targets support embedding flags to the linker inside individual object
53585394 files. Typically this is used in conjunction with language extensions which
53595395 allow source files to explicitly declare the libraries they depend on, and have
53605396 these automatically be transmitted to the linker via object files.
53615397
5362 These flags are encoded in the IR using metadata in the module flags section,
5363 using the ``Linker Options`` key. The merge behavior for this flag is required
5364 to be ``AppendUnique``, and the value for the key is expected to be a metadata
5365 node which should be a list of other metadata nodes, each of which should be a
5398 These flags are encoded in the IR using named metadata with the name
5399 ``!llvm.linker.options``. Each operand is expected to be a metadata node
5400 which should be a list of other metadata nodes, each of which should be a
53665401 list of metadata strings defining linker options.
53675402
53685403 For example, the following metadata section specifies two separate sets of
53695404 linker options, presumably to link against ``libz`` and the ``Cocoa``
53705405 framework::
53715406
5372 !0 = !{ i32 6, !"Linker Options",
5373 !{
5374 !{ !"-lz" },
5375 !{ !"-framework", !"Cocoa" } } }
5376 !llvm.module.flags = !{ !0 }
5407 !0 = !{ !"-lz" },
5408 !1 = !{ !"-framework", !"Cocoa" } } }
5409 !llvm.linker.options = !{ !0, !1 }
53775410
53785411 The metadata encoding as lists of lists of options, as opposed to a collapsed
53795412 list of options, is chosen so that the IR encoding can use multiple option
53845417 Each individual option is required to be either a valid option for the target's
53855418 linker, or an option that is reserved by the target specific assembly writer or
53865419 object file emitter. No other aspect of these options is defined by the IR.
5387
5388 C type width Module Flags Metadata
5389 ----------------------------------
5390
5391 The ARM backend emits a section into each generated object file describing the
5392 options that it was compiled with (in a compiler-independent way) to prevent
5393 linking incompatible objects, and to allow automatic library selection. Some
5394 of these options are not visible at the IR level, namely wchar_t width and enum
5395 width.
5396
5397 To pass this information to the backend, these options are encoded in module
5398 flags metadata, using the following key-value pairs:
5399
5400 .. list-table::
5401 :header-rows: 1
5402 :widths: 30 70
5403
5404 * - Key
5405 - Value
5406
5407 * - short_wchar
5408 - * 0 --- sizeof(wchar_t) == 4
5409 * 1 --- sizeof(wchar_t) == 2
5410
5411 * - short_enum
5412 - * 0 --- Enums are at least as large as an ``int``.
5413 * 1 --- Enums are stored in the smallest integer type which can
5414 represent all of its values.
5415
5416 For example, the following metadata section specifies that the module was
5417 compiled with a ``wchar_t`` width of 4 bytes, and the underlying type of an
5418 enum is the smallest type which can represent all of its values::
5419
5420 !llvm.module.flags = !{!0, !1}
5421 !0 = !{i32 1, !"short_wchar", i32 1}
5422 !1 = !{i32 1, !"short_enum", i32 0}
54235420
54245421 .. _intrinsicglobalvariables:
54255422
4141 ~TargetLoweringObjectFileELF() override = default;
4242
4343 /// Emit Obj-C garbage collection and linker options.
44 void emitModuleFlags(MCStreamer &Streamer,
45 ArrayRef ModuleFlags,
46 const TargetMachine &TM) const override;
44 void emitModuleMetadata(MCStreamer &Streamer, Module &M,
45 const TargetMachine &TM) const override;
4746
4847 void emitPersonalityValue(MCStreamer &Streamer, const DataLayout &TM,
4948 const MCSymbol *Sym) const override;
9897 void Initialize(MCContext &Ctx, const TargetMachine &TM) override;
9998
10099 /// Emit the module flags that specify the garbage collection information.
101 void emitModuleFlags(MCStreamer &Streamer,
102 ArrayRef ModuleFlags,
103 const TargetMachine &TM) const override;
100 void emitModuleMetadata(MCStreamer &Streamer, Module &M,
101 const TargetMachine &TM) const override;
104102
105103 MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
106104 const TargetMachine &TM) const override;
154152 const TargetMachine &TM) const override;
155153
156154 /// Emit Obj-C garbage collection and linker options.
157 void emitModuleFlags(MCStreamer &Streamer,
158 ArrayRef ModuleFlags,
159 const TargetMachine &TM) const override;
155 void emitModuleMetadata(MCStreamer &Streamer, Module &M,
156 const TargetMachine &TM) const override;
160157
161158 MCSection *getStaticCtorSection(unsigned Priority,
162159 const MCSymbol *KeySym) const override;
157157
158158 private:
159159 /// Parse metadata from the module
160 // FIXME: it only parses "Linker Options" metadata at the moment
160 // FIXME: it only parses "llvm.linker.options" metadata at the moment
161161 void parseMetadata();
162162
163163 /// Parse the symbols from the module and model-level ASM and add them to
6969 virtual void emitPersonalityValue(MCStreamer &Streamer, const DataLayout &TM,
7070 const MCSymbol *Sym) const;
7171
72 /// Emit the module flags that the platform cares about.
73 virtual void emitModuleFlags(MCStreamer &Streamer,
74 ArrayRef Flags,
75 const TargetMachine &TM) const {}
72 /// Emit the module-level metadata that the platform cares about.
73 virtual void emitModuleMetadata(MCStreamer &Streamer, Module &M,
74 const TargetMachine &TM) const {}
7675
7776 /// Given a constant with the SectionKind, return a section that it should be
7877 /// placed in.
26072607 if (Error Err = MDLoader->parseModuleMetadata())
26082608 return Err;
26092609 }
2610
2611 // Upgrade "Linker Options" module flag to "llvm.linker.options" module-level
2612 // metadata.
2613 if (Metadata *Val = TheModule->getModuleFlag("Linker Options")) {
2614 NamedMDNode *LinkerOpts =
2615 TheModule->getOrInsertNamedMetadata("llvm.linker.options");
2616 for (const MDOperand &MDOptions : cast(Val)->operands())
2617 LinkerOpts->addOperand(cast(MDOptions));
2618 }
2619
26102620 DeferredMetadataInfo.clear();
26112621 return Error::success();
26122622 }
12851285
12861286 const TargetLoweringObjectFile &TLOF = getObjFileLowering();
12871287
1288 // Emit module flags.
1289 SmallVector ModuleFlags;
1290 M.getModuleFlagsMetadata(ModuleFlags);
1291 if (!ModuleFlags.empty())
1292 TLOF.emitModuleFlags(*OutStreamer, ModuleFlags, TM);
1288 TLOF.emitModuleMetadata(*OutStreamer, M, TM);
12931289
12941290 if (TM.getTargetTriple().isOSBinFormatELF()) {
12951291 MachineModuleInfoELF &MMIELF = MMI->getObjFileInfo();
6060 using namespace llvm;
6161 using namespace dwarf;
6262
63 static void GetObjCImageInfo(ArrayRef ModuleFlags,
64 unsigned &Version, unsigned &Flags,
63 static void GetObjCImageInfo(Module &M, unsigned &Version, unsigned &Flags,
6564 StringRef &Section) {
65 SmallVector ModuleFlags;
66 M.getModuleFlagsMetadata(ModuleFlags);
67
6668 for (const auto &MFE: ModuleFlags) {
6769 // Ignore flags with 'Require' behaviour.
6870 if (MFE.Behavior == Module::Require)
8789 // ELF
8890 //===----------------------------------------------------------------------===//
8991
90 void TargetLoweringObjectFileELF::emitModuleFlags(
91 MCStreamer &Streamer, ArrayRef ModuleFlags,
92 const TargetMachine &TM) const {
92 void TargetLoweringObjectFileELF::emitModuleMetadata(
93 MCStreamer &Streamer, Module &M, const TargetMachine &TM) const {
9394 unsigned Version = 0;
9495 unsigned Flags = 0;
9596 StringRef Section;
9697
97 GetObjCImageInfo(ModuleFlags, Version, Flags, Section);
98 GetObjCImageInfo(M, Version, Flags, Section);
9899 if (Section.empty())
99100 return;
100101
617618 }
618619 }
619620
620 /// emitModuleFlags - Perform code emission for module flags.
621 void TargetLoweringObjectFileMachO::emitModuleFlags(
622 MCStreamer &Streamer, ArrayRef ModuleFlags,
623 const TargetMachine &TM) const {
624 MDNode *LinkerOptions = nullptr;
625
626 for (const auto &MFE : ModuleFlags) {
627 StringRef Key = MFE.Key->getString();
628 if (Key == "Linker Options")
629 LinkerOptions = cast(MFE.Val);
630 }
631
621 void TargetLoweringObjectFileMachO::emitModuleMetadata(
622 MCStreamer &Streamer, Module &M, const TargetMachine &TM) const {
632623 // Emit the linker options if present.
633 if (LinkerOptions) {
624 if (auto *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) {
634625 for (const auto &Option : LinkerOptions->operands()) {
635626 SmallVector StrOptions;
636627 for (const auto &Piece : cast(Option)->operands())
642633 unsigned VersionVal = 0;
643634 unsigned ImageInfoFlags = 0;
644635 StringRef SectionVal;
645 GetObjCImageInfo(ModuleFlags, VersionVal, ImageInfoFlags, SectionVal);
636
637 GetObjCImageInfo(M, VersionVal, ImageInfoFlags, SectionVal);
646638
647639 // The section is mandatory. If we don't have it, then we don't have GC info.
648640 if (SectionVal.empty())
11581150 COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE, UniqueID);
11591151 }
11601152
1161 void TargetLoweringObjectFileCOFF::emitModuleFlags(
1162 MCStreamer &Streamer, ArrayRef ModuleFlags,
1163 const TargetMachine &TM) const {
1164 MDNode *LinkerOptions = nullptr;
1165
1166 for (const auto &MFE : ModuleFlags) {
1167 StringRef Key = MFE.Key->getString();
1168 if (Key == "Linker Options")
1169 LinkerOptions = cast(MFE.Val);
1170 }
1171
1172 if (LinkerOptions) {
1153 void TargetLoweringObjectFileCOFF::emitModuleMetadata(
1154 MCStreamer &Streamer, Module &M, const TargetMachine &TM) const {
1155 if (NamedMDNode *LinkerOptions = M.getNamedMetadata("llvm.linker.options")) {
11731156 // Emit the linker options to the linker .drectve section. According to the
11741157 // spec, this section is a space-separated string containing flags for
11751158 // linker.
11891172 unsigned Flags = 0;
11901173 StringRef Section;
11911174
1192 GetObjCImageInfo(ModuleFlags, Version, Flags, Section);
1175 GetObjCImageInfo(M, Version, Flags, Section);
11931176 if (Section.empty())
11941177 return;
11951178
13291329 = mdconst::dyn_extract_or_null(Op->getOperand(2));
13301330 Assert(Value, "wchar_size metadata requires constant integer argument");
13311331 }
1332
1333 if (ID->getString() == "Linker Options") {
1334 // If the llvm.linker.options named metadata exists, we assume that the
1335 // bitcode reader has upgraded the module flag. Otherwise the flag might
1336 // have been created by a client directly.
1337 Assert(M.getNamedMetadata("llvm.linker.options"),
1338 "'Linker Options' named metadata no longer supported");
1339 }
13321340 }
13331341
13341342 /// Return true if this attribute kind only applies to functions.
636636 raw_string_ostream OS(LinkerOpts);
637637
638638 // Linker Options
639 if (Metadata *Val = getModule().getModuleFlag("Linker Options")) {
640 MDNode *LinkerOptions = cast(Val);
639 if (NamedMDNode *LinkerOptions =
640 getModule().getNamedMetadata("llvm.linker.options")) {
641641 for (unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) {
642 MDNode *MDOptions = cast(LinkerOptions->getOperand(i));
642 MDNode *MDOptions = LinkerOptions->getOperand(i);
643643 for (unsigned ii = 0, ie = MDOptions->getNumOperands(); ii != ie; ++ii) {
644644 MDString *MDOption = cast(MDOptions->getOperand(ii));
645645 OS << " " << MDOption->getString();
108108 if (TT.isOSBinFormatCOFF()) {
109109 if (auto E = M->materializeMetadata())
110110 return E;
111 if (Metadata *Val = M->getModuleFlag("Linker Options")) {
112 MDNode *LinkerOptions = cast(Val);
113 for (const MDOperand &MDOptions : LinkerOptions->operands())
111 if (NamedMDNode *LinkerOptions =
112 M->getNamedMetadata("llvm.linker.options")) {
113 for (MDNode *MDOptions : LinkerOptions->operands())
114114 for (const MDOperand &MDOption : cast(MDOptions)->operands())
115115 COFFLinkerOptsOS << " " << cast(MDOption)->getString();
116116 }
0 ; RUN: llvm-as -disable-verify < %s | llvm-dis | FileCheck %s
1 ; RUN: not llvm-as < %s 2>&1 | FileCheck --check-prefix=ERROR %s
2
3 ; CHECK: !llvm.linker.options = !{!2, !3}
4 ; CHECK: !2 = !{!"/DEFAULTLIB:libcmtd.lib"}
5 ; CHECK: !3 = !{!"/DEFAULTLIB:oldnames.lib"}
6
7 ; ERROR: 'Linker Options' named metadata no longer supported
8
9 !0 = !{i32 6, !"Linker Options", !1}
10 !1 = !{!2, !3}
11 !2 = !{!"/DEFAULTLIB:libcmtd.lib"}
12 !3 = !{!"/DEFAULTLIB:oldnames.lib"}
13
14 !llvm.module.flags = !{!0}
6464
6565 !llvm.dbg.cu = !{!2, !11}
6666 !llvm.ident = !{!13, !13}
67 !llvm.module.flags = !{!14, !18, !19, !20}
67 !llvm.module.flags = !{!18, !19, !20}
6868
6969 !0 = !DIGlobalVariableExpression(var: !1)
7070 !1 = distinct !DIGlobalVariable(name: "a", linkageName: "\01?a@@3TYYSTYPE@@A", scope: !2, file: !3, line: 2, type: !6, isLocal: false, isDefinition: true)
8080 !11 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !12, producer: "clang version 5.0.0 ", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !4)
8181 !12 = !DIFile(filename: "b.cpp", directory: "C:\5Csrc\5Cllvm-project\5Cbuild", checksumkind: CSK_MD5, checksum: "9cfd390d8827beab36769147bb037abc")
8282 !13 = !{!"clang version 5.0.0 "}
83 !14 = !{i32 6, !"Linker Options", !15}
84 !15 = !{!16, !17}
85 !16 = !{!"/DEFAULTLIB:libcmt.lib"}
86 !17 = !{!"/DEFAULTLIB:oldnames.lib"}
8783 !18 = !{i32 2, !"CodeView", i32 1}
8884 !19 = !{i32 2, !"Debug Info Version", i32 3}
8985 !20 = !{i32 1, !"PIC Level", i32 2}
3838 ret void
3939 }
4040
41 !llvm.module.flags = !{!0, !1, !2}
41 !llvm.module.flags = !{!0, !1}
4242 !llvm.dbg.cu = !{!4}
4343
4444 !0 = !{i32 2, !"CodeView", i32 1}
4545 !1 = !{i32 2, !"Debug Info Version", i32 3}
46 !2 = !{i32 6, !"Linker Options", !{}}
4746 !4 = distinct !DICompileUnit(language: DW_LANG_D, file: !5, producer: "LDC (http://wiki.dlang.org/LDC)", isOptimized: false, runtimeVersion: 1, emissionKind: FullDebug)
4847 !5 = !DIFile(filename: "opover2.d", directory: "C:\5CLDC\5Cninja-ldc\5C..\5Cldc\5Ctests\5Cd2\5Cdmd-testsuite\5Crunnable")
4948 !6 = !DILocation(line: 302, column: 9, scope: !7, inlinedAt: !15)
55 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
66
77 ; CHECK-NOT: linker opts:
8 !0 = !{i32 6, !"Linker Options", !{!{!"/include:foo"}}}
9 !llvm.module.flags = !{ !0 }
8 !0 = !{!"/include:foo"}
9 !llvm.linker.options = !{ !0 }
1010
1111 @g1 = global i32 0
1212
88 source_filename = "src.c"
99
1010 ; CHECK: linker opts: /include:foo
11 !0 = !{i32 6, !"Linker Options", !{!{!"/include:foo"}}}
12 !llvm.module.flags = !{ !0 }
11 !0 = !{!"/include:foo"}
12 !llvm.linker.options = !{ !0 }
1313
1414 ; CHECK: D------X _fun
1515 define i32 @fun() {
1212 attributes #0 = { nounwind sspstrong "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="pentium4" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
1313
1414 !llvm.dbg.cu = !{!0}
15 !llvm.module.flags = !{!3, !7, !8}
15 !llvm.module.flags = !{!7, !8}
1616 !llvm.ident = !{!9}
1717
1818 !0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 4.0.0 ", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
3434 ; CHECK-NOT: .short 4412 # Record kind: S_COMPILE3
3535 !1 = !DIFile(filename: "D:\5Csrc\5Cscopes\5Cfoo.cpp", directory: "D:\5Csrc\5Cscopes\5Cclang")
3636 !2 = !{}
37 !3 = !{i32 6, !"Linker Options", !4}
38 !4 = !{!5, !6}
39 !5 = !{!"/DEFAULTLIB:libcmtd.lib"}
40 !6 = !{!"/DEFAULTLIB:oldnames.lib"}
4137 !7 = !{i32 2, !"CodeView", i32 1}
4238 !8 = !{i32 2, !"Debug Info Version", i32 3}
4339 !9 = !{!"clang version 4.0.0 "}
0 ; RUN: llc -O0 -mtriple=i386-pc-win32 -filetype=asm -o - %s | FileCheck %s
11
2 !0 = !{i32 6, !"Linker Options", !{!{!"/DEFAULTLIB:msvcrt.lib"}, !{!"/DEFAULTLIB:msvcrt.lib", !"/DEFAULTLIB:secur32.lib"}, !{!"/DEFAULTLIB:\22C:\5Cpath to\5Casan_rt.lib\22"}, !{!"\22/with spaces\22"}}}
3
4 !llvm.module.flags = !{ !0 }
2 !0 = !{!"/DEFAULTLIB:msvcrt.lib"}
3 !1 = !{!"/DEFAULTLIB:msvcrt.lib", !"/DEFAULTLIB:secur32.lib"}
4 !2 = !{!"/DEFAULTLIB:\22C:\5Cpath to\5Casan_rt.lib\22"}
5 !3 = !{!"\22/with spaces\22"}
6 !llvm.linker.options = !{!0, !1, !2, !3}
57
68 define dllexport void @foo() {
79 ret void
2626 ; CHECK-OBJ: ]
2727 ; CHECK-OBJ: }
2828
29 !0 = !{i32 6, !"Linker Options", !{!{!"-lz"}, !{!"-framework", !"Cocoa"}, !{!"-lmath"}}}
30
31 !llvm.module.flags = !{ !0 }
29 !0 = !{!"-lz"}
30 !1 = !{!"-framework", !"Cocoa"}
31 !2 = !{!"-lmath"}
32 !llvm.linker.options = !{!0, !1, !2}