llvm.org GIT mirror llvm / a747cf1
Adding new LTO APIs to parse metadata nodes and extract linker options and dependent libraries from a bitcode module. Differential Revision: http://llvm-reviews.chandlerc.com/D2343 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@199759 91177308-0d34-0410-b5e6-96231b3b80d8 Yunzhong Gao 5 years ago
8 changed file(s) with 156 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
7979 public:
8080 virtual ~TargetLoweringObjectFileMachO() {}
8181
82 /// getDepLibFromLinkerOpt - Extract the dependent library name from a linker
83 /// option string. Returns StringRef() if the option does not specify a library.
84 virtual StringRef getDepLibFromLinkerOpt(StringRef LinkerOption) const;
85
8286 /// emitModuleFlags - Emit the module flags that specify the garbage
8387 /// collection information.
8488 virtual void emitModuleFlags(MCStreamer &Streamer,
128132 SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
129133 Mangler *Mang, const TargetMachine &TM) const;
130134
135 /// getDepLibFromLinkerOpt - Extract the dependent library name from a linker
136 /// option string. Returns StringRef() if the option does not specify a library.
137 virtual StringRef getDepLibFromLinkerOpt(StringRef LinkerOption) const;
138
131139 /// emitModuleFlags - Emit Obj-C garbage collection and linker options. Only
132140 /// linker option emission is implemented for COFF.
133141 virtual void emitModuleFlags(MCStreamer &Streamer,
5050 llvm::OwningPtr _module;
5151 llvm::OwningPtr _target;
5252 llvm::MCObjectFileInfo ObjFileInfo;
53 StringSet _linkeropt_strings;
54 std::vector _deplibs;
55 std::vector _linkeropts;
5356 std::vector _symbols;
5457
5558 // _defines and _undefines only needed to disambiguate tentative definitions
128131 return NULL;
129132 }
130133
134 /// getDependentLibraryCount - Get the number of dependent libraries
135 uint32_t getDependentLibraryCount() {
136 return _deplibs.size();
137 }
138
139 /// getDependentLibrary - Get the dependent library at the specified index.
140 const char *getDependentLibrary(uint32_t index) {
141 if (index < _deplibs.size())
142 return _deplibs[index];
143 return NULL;
144 }
145
146 /// getLinkerOptCount - Get the number of linker options
147 uint32_t getLinkerOptCount() {
148 return _linkeropts.size();
149 }
150
151 /// getLinkerOpt - Get the linker option at the specified index.
152 const char *getLinkerOpt(uint32_t index) {
153 if (index < _linkeropts.size())
154 return _linkeropts[index];
155 return NULL;
156 }
157
131158 /// getLLVVMModule - Return the Module.
132159 llvm::Module *getLLVVMModule() { return _module.get(); }
133160
137164 }
138165
139166 private:
167 /// parseMetadata - Parse metadata from the module
168 // FIXME: it only parses "Linker Options" metadata at the moment
169 void parseMetadata();
170
140171 /// parseSymbols - Parse the symbols from the module and model-level ASM and
141172 /// add them to either the defined or undefined lists.
142173 bool parseSymbols(std::string &errMsg);
5555 virtual void emitPersonalityValue(MCStreamer &Streamer,
5656 const TargetMachine &TM,
5757 const MCSymbol *Sym) const;
58
59 /// getDepLibFromLinkerOpt - Extract the dependent library name from a linker
60 /// option string. Returns StringRef() if the option does not specify a library.
61 virtual StringRef getDepLibFromLinkerOpt(StringRef LinkerOption) const {
62 return StringRef();
63 }
5864
5965 /// emitModuleFlags - Emit the module flags that the platform cares about.
6066 virtual void emitModuleFlags(MCStreamer &,
3939 * @{
4040 */
4141
42 #define LTO_API_VERSION 7
42 #define LTO_API_VERSION 8
4343
4444 /**
4545 * \since prior to LTO_API_VERSION=3
245245 extern lto_symbol_attributes
246246 lto_module_get_symbol_attribute(lto_module_t mod, unsigned int index);
247247
248
249 /**
250 * Returns the number of dependent libraries in the object module.
251 *
252 * \since LTO_API_VERSION=8
253 */
254 extern unsigned int
255 lto_module_get_num_deplibs(lto_module_t mod);
256
257
258 /**
259 * Returns the ith dependent library in the module.
260 *
261 * \since LTO_API_VERSION=8
262 */
263 extern const char*
264 lto_module_get_deplib(lto_module_t mod, unsigned int index);
265
266
267 /**
268 * Returns the number of linker options in the object module.
269 *
270 * \since LTO_API_VERSION=8
271 */
272 extern unsigned int
273 lto_module_get_num_linkeropts(lto_module_t mod);
274
275
276 /**
277 * Returns the ith linker option in the module.
278 *
279 * \since LTO_API_VERSION=8
280 */
281 extern const char*
282 lto_module_get_linkeropt(lto_module_t mod, unsigned int index);
283
284
248285 /**
249286 * Diagnostic severity.
250287 *
400400 //===----------------------------------------------------------------------===//
401401 // MachO
402402 //===----------------------------------------------------------------------===//
403
404 /// getDepLibFromLinkerOpt - Extract the dependent library name from a linker
405 /// option string. Returns StringRef() if the option does not specify a library.
406 StringRef TargetLoweringObjectFileMachO::
407 getDepLibFromLinkerOpt(StringRef LinkerOption) const {
408 const char *LibCmd = "-l";
409 if (LinkerOption.startswith(LibCmd))
410 return LinkerOption.substr(strlen(LibCmd));
411 return StringRef();
412 }
403413
404414 /// emitModuleFlags - Perform code emission for module flags.
405415 void TargetLoweringObjectFileMachO::
773783 return DataSection;
774784 }
775785
786 StringRef TargetLoweringObjectFileCOFF::
787 getDepLibFromLinkerOpt(StringRef LinkerOption) const {
788 const char *LibCmd = "/DEFAULTLIB:";
789 if (LinkerOption.startswith(LibCmd))
790 return LinkerOption.substr(strlen(LibCmd));
791 return StringRef();
792 }
793
776794 void TargetLoweringObjectFileCOFF::
777795 emitModuleFlags(MCStreamer &Streamer,
778796 ArrayRef ModuleFlags,
1717 #include "llvm/Bitcode/ReaderWriter.h"
1818 #include "llvm/IR/Constants.h"
1919 #include "llvm/IR/LLVMContext.h"
20 #include "llvm/IR/Metadata.h"
2021 #include "llvm/IR/Module.h"
2122 #include "llvm/MC/MCExpr.h"
2223 #include "llvm/MC/MCInst.h"
3637 #include "llvm/Support/TargetRegistry.h"
3738 #include "llvm/Support/TargetSelect.h"
3839 #include "llvm/Support/system_error.h"
40 #include "llvm/Target/TargetLowering.h"
41 #include "llvm/Target/TargetLoweringObjectFile.h"
3942 #include "llvm/Target/TargetRegisterInfo.h"
4043 #include "llvm/Transforms/Utils/GlobalStatus.h"
4144 using namespace llvm;
175178 delete Ret;
176179 return NULL;
177180 }
181
182 Ret->parseMetadata();
178183
179184 return Ret;
180185 }
797802
798803 return false;
799804 }
805
806 /// parseMetadata - Parse metadata from the module
807 void LTOModule::parseMetadata() {
808 // Linker Options
809 if (Value *Val = _module->getModuleFlag("Linker Options")) {
810 MDNode *LinkerOptions = cast(Val);
811 for (unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) {
812 MDNode *MDOptions = cast(LinkerOptions->getOperand(i));
813 for (unsigned ii = 0, ie = MDOptions->getNumOperands(); ii != ie; ++ii) {
814 MDString *MDOption = cast(MDOptions->getOperand(ii));
815 StringRef Op = _linkeropt_strings.
816 GetOrCreateValue(MDOption->getString()).getKey();
817 StringRef DepLibName = _target->getTargetLowering()->
818 getObjFileLowering().getDepLibFromLinkerOpt(Op);
819 if (!DepLibName.empty())
820 _deplibs.push_back(DepLibName.data());
821 else if (!Op.empty())
822 _linkeropts.push_back(Op.data());
823 }
824 }
825 }
826
827 // Add other interesting metadata here.
828 }
192192 return mod->getSymbolAttributes(index);
193193 }
194194
195 /// lto_module_get_num_deplibs - Returns the number of dependent libraries in
196 /// the object module.
197 unsigned int lto_module_get_num_deplibs(lto_module_t mod) {
198 return mod->getDependentLibraryCount();
199 }
200
201 /// lto_module_get_deplib - Returns the ith dependent library in the module.
202 const char* lto_module_get_deplib(lto_module_t mod, unsigned int index) {
203 return mod->getDependentLibrary(index);
204 }
205
206 /// lto_module_get_num_linkeropts - Returns the number of linker options in the
207 /// object module.
208 unsigned int lto_module_get_num_linkeropts(lto_module_t mod) {
209 return mod->getLinkerOptCount();
210 }
211
212 /// lto_module_get_linkeropt - Returns the ith linker option in the module.
213 const char* lto_module_get_linkeropt(lto_module_t mod, unsigned int index) {
214 return mod->getLinkerOpt(index);
215 }
216
195217 /// Set a diagnostic handler.
196218 void lto_codegen_set_diagnostic_handler(lto_code_gen_t cg,
197219 lto_diagnostic_handler_t diag_handler,
44 lto_module_create_from_fd
55 lto_module_create_from_fd_at_offset
66 lto_module_create_from_memory
7 lto_module_get_deplib
8 lto_module_get_linkeropt
9 lto_module_get_num_deplibs
10 lto_module_get_num_linkeropts
711 lto_module_get_num_symbols
812 lto_module_get_symbol_attribute
913 lto_module_get_symbol_name