blob: d960617fdc7896ab378f940983f7a2f369bbc322 [file] [log] [blame]
Nick Kledzik77595fc2008-02-26 20:26:43 +00001/*===-- llvm-c/lto.h - LTO Public C Interface ---------------------*- C -*-===*\
2|* *|
3|* The LLVM Compiler Infrastructure *|
4|* *|
5|* This file is distributed under the University of Illinois Open Source *|
6|* License. See LICENSE.TXT for details. *|
7|* *|
8|*===----------------------------------------------------------------------===*|
9|* *|
10|* This header provides public interface to an abstract link time optimization*|
11|* library. LLVM provides an implementation of this interface for use with *|
12|* llvm bitcode files. *|
13|* *|
14\*===----------------------------------------------------------------------===*/
15
Jakub Staszak674be022013-01-10 00:45:19 +000016#ifndef LLVM_C_LTO_H
17#define LLVM_C_LTO_H
Nick Kledzik77595fc2008-02-26 20:26:43 +000018
Peter Collingbourneaca74f02013-09-25 07:11:58 +000019#ifndef __cplusplus
Nick Kledzik77595fc2008-02-26 20:26:43 +000020#include <stdbool.h>
Peter Collingbourneaca74f02013-09-25 07:11:58 +000021#endif
Nick Kledzik77595fc2008-02-26 20:26:43 +000022#include <stddef.h>
Rafael Espindolab4cc0312011-02-08 22:40:47 +000023#include <unistd.h>
Nick Kledzik77595fc2008-02-26 20:26:43 +000024
Gregory Szorc6244b512012-03-21 03:54:29 +000025/**
26 * @defgroup LLVMCLTO LTO
27 * @ingroup LLVMC
28 *
29 * @{
30 */
31
Shuxin Yang00c19802013-08-27 17:15:54 +000032#define LTO_API_VERSION 4
Nick Kledzikcbad5862009-06-04 00:28:45 +000033
Nick Kledzik77595fc2008-02-26 20:26:43 +000034typedef enum {
Bill Wendling7afea0c2010-09-27 20:17:45 +000035 LTO_SYMBOL_ALIGNMENT_MASK = 0x0000001F, /* log2 of alignment */
Bill Wendling168f1422012-03-31 10:44:20 +000036 LTO_SYMBOL_PERMISSIONS_MASK = 0x000000E0,
37 LTO_SYMBOL_PERMISSIONS_CODE = 0x000000A0,
38 LTO_SYMBOL_PERMISSIONS_DATA = 0x000000C0,
39 LTO_SYMBOL_PERMISSIONS_RODATA = 0x00000080,
40 LTO_SYMBOL_DEFINITION_MASK = 0x00000700,
41 LTO_SYMBOL_DEFINITION_REGULAR = 0x00000100,
42 LTO_SYMBOL_DEFINITION_TENTATIVE = 0x00000200,
43 LTO_SYMBOL_DEFINITION_WEAK = 0x00000300,
44 LTO_SYMBOL_DEFINITION_UNDEFINED = 0x00000400,
Bill Wendling7afea0c2010-09-27 20:17:45 +000045 LTO_SYMBOL_DEFINITION_WEAKUNDEF = 0x00000500,
Bill Wendling168f1422012-03-31 10:44:20 +000046 LTO_SYMBOL_SCOPE_MASK = 0x00003800,
47 LTO_SYMBOL_SCOPE_INTERNAL = 0x00000800,
48 LTO_SYMBOL_SCOPE_HIDDEN = 0x00001000,
49 LTO_SYMBOL_SCOPE_PROTECTED = 0x00002000,
Bill Wendling7afea0c2010-09-27 20:17:45 +000050 LTO_SYMBOL_SCOPE_DEFAULT = 0x00001800,
51 LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN = 0x00002800
Nick Kledzik77595fc2008-02-26 20:26:43 +000052} lto_symbol_attributes;
53
54typedef enum {
55 LTO_DEBUG_MODEL_NONE = 0,
56 LTO_DEBUG_MODEL_DWARF = 1
57} lto_debug_model;
58
59typedef enum {
60 LTO_CODEGEN_PIC_MODEL_STATIC = 0,
61 LTO_CODEGEN_PIC_MODEL_DYNAMIC = 1,
62 LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC = 2
63} lto_codegen_model;
64
65
66/** opaque reference to a loaded object module */
67typedef struct LTOModule* lto_module_t;
68
69/** opaque reference to a code generator */
70typedef struct LTOCodeGenerator* lto_code_gen_t;
71
Nick Kledzik77595fc2008-02-26 20:26:43 +000072#ifdef __cplusplus
73extern "C" {
74#endif
75
76/**
77 * Returns a printable string.
78 */
79extern const char*
Gordon Henriksen16c1f442008-05-04 12:55:34 +000080lto_get_version(void);
Nick Kledzik77595fc2008-02-26 20:26:43 +000081
82
83/**
Chris Lattner7a2bdde2011-04-15 05:18:47 +000084 * Returns the last error string or NULL if last operation was successful.
Nick Kledzik77595fc2008-02-26 20:26:43 +000085 */
86extern const char*
Gordon Henriksen16c1f442008-05-04 12:55:34 +000087lto_get_error_message(void);
Nick Kledzik77595fc2008-02-26 20:26:43 +000088
Nick Kledzik77595fc2008-02-26 20:26:43 +000089/**
90 * Checks if a file is a loadable object file.
91 */
92extern bool
93lto_module_is_object_file(const char* path);
94
95
96/**
97 * Checks if a file is a loadable object compiled for requested target.
98 */
99extern bool
Bill Wendling168f1422012-03-31 10:44:20 +0000100lto_module_is_object_file_for_target(const char* path,
Nick Lewyckyb454eab2009-02-06 07:01:00 +0000101 const char* target_triple_prefix);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000102
103
104/**
105 * Checks if a buffer is a loadable object file.
106 */
107extern bool
108lto_module_is_object_file_in_memory(const void* mem, size_t length);
109
110
111/**
112 * Checks if a buffer is a loadable object compiled for requested target.
113 */
114extern bool
Bill Wendling168f1422012-03-31 10:44:20 +0000115lto_module_is_object_file_in_memory_for_target(const void* mem, size_t length,
Eric Christopherca3ca132010-07-12 05:13:35 +0000116 const char* target_triple_prefix);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000117
118
119/**
120 * Loads an object file from disk.
121 * Returns NULL on error (check lto_get_error_message() for details).
122 */
123extern lto_module_t
Owen Anderson0e7a5462009-07-02 00:31:14 +0000124lto_module_create(const char* path);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000125
126
127/**
128 * Loads an object file from memory.
129 * Returns NULL on error (check lto_get_error_message() for details).
130 */
131extern lto_module_t
Owen Anderson0e7a5462009-07-02 00:31:14 +0000132lto_module_create_from_memory(const void* mem, size_t length);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000133
Rafael Espindolab4cc0312011-02-08 22:40:47 +0000134/**
135 * Loads an object file from disk. The seek point of fd is not preserved.
136 * Returns NULL on error (check lto_get_error_message() for details).
137 */
138extern lto_module_t
Rafael Espindolaf21b1052011-03-17 00:36:11 +0000139lto_module_create_from_fd(int fd, const char *path, size_t file_size);
140
141/**
142 * Loads an object file from disk. The seek point of fd is not preserved.
143 * Returns NULL on error (check lto_get_error_message() for details).
144 */
145extern lto_module_t
146lto_module_create_from_fd_at_offset(int fd, const char *path, size_t file_size,
147 size_t map_size, off_t offset);
Rafael Espindolab4cc0312011-02-08 22:40:47 +0000148
Nick Kledzik77595fc2008-02-26 20:26:43 +0000149
150/**
151 * Frees all memory internally allocated by the module.
152 * Upon return the lto_module_t is no longer valid.
153 */
154extern void
155lto_module_dispose(lto_module_t mod);
156
157
158/**
159 * Returns triple string which the object module was compiled under.
160 */
161extern const char*
162lto_module_get_target_triple(lto_module_t mod);
163
Rafael Espindolacbb170d2010-08-09 21:09:46 +0000164/**
165 * Sets triple string with which the object will be codegened.
166 */
167extern void
168lto_module_set_target_triple(lto_module_t mod, const char *triple);
169
Nick Kledzik77595fc2008-02-26 20:26:43 +0000170
171/**
172 * Returns the number of symbols in the object module.
173 */
Devang Patel6a6623c2011-01-07 22:26:25 +0000174extern unsigned int
Nick Kledzik77595fc2008-02-26 20:26:43 +0000175lto_module_get_num_symbols(lto_module_t mod);
176
177
178/**
179 * Returns the name of the ith symbol in the object module.
180 */
181extern const char*
Devang Patel6a6623c2011-01-07 22:26:25 +0000182lto_module_get_symbol_name(lto_module_t mod, unsigned int index);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000183
184
185/**
186 * Returns the attributes of the ith symbol in the object module.
187 */
188extern lto_symbol_attributes
Devang Patel6a6623c2011-01-07 22:26:25 +0000189lto_module_get_symbol_attribute(lto_module_t mod, unsigned int index);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000190
191
192/**
193 * Instantiates a code generator.
194 * Returns NULL on error (check lto_get_error_message() for details).
195 */
196extern lto_code_gen_t
Owen Anderson0e7a5462009-07-02 00:31:14 +0000197lto_codegen_create(void);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000198
199
200/**
201 * Frees all code generator and all memory it internally allocated.
202 * Upon return the lto_code_gen_t is no longer valid.
203 */
204extern void
205lto_codegen_dispose(lto_code_gen_t);
206
207
208
209/**
210 * Add an object module to the set of modules for which code will be generated.
211 * Returns true on error (check lto_get_error_message() for details).
212 */
213extern bool
214lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod);
215
216
217
218/**
219 * Sets if debug info should be generated.
220 * Returns true on error (check lto_get_error_message() for details).
221 */
222extern bool
223lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model);
224
225
226/**
227 * Sets which PIC code model to generated.
228 * Returns true on error (check lto_get_error_message() for details).
229 */
230extern bool
231lto_codegen_set_pic_model(lto_code_gen_t cg, lto_codegen_model);
232
233
234/**
Rafael Espindola2d643ef2010-08-11 00:15:13 +0000235 * Sets the cpu to generate code for.
236 */
237extern void
238lto_codegen_set_cpu(lto_code_gen_t cg, const char *cpu);
239
240
241/**
Nick Kledzikcbad5862009-06-04 00:28:45 +0000242 * Sets the location of the assembler tool to run. If not set, libLTO
243 * will use gcc to invoke the assembler.
244 */
245extern void
246lto_codegen_set_assembler_path(lto_code_gen_t cg, const char* path);
247
Rafael Espindola98197e52010-08-10 18:55:09 +0000248/**
249 * Sets extra arguments that libLTO should pass to the assembler.
250 */
251extern void
252lto_codegen_set_assembler_args(lto_code_gen_t cg, const char **args,
253 int nargs);
Nick Kledzikcbad5862009-06-04 00:28:45 +0000254
255/**
Nick Kledzik77595fc2008-02-26 20:26:43 +0000256 * Adds to a list of all global symbols that must exist in the final
257 * generated code. If a function is not listed, it might be
258 * inlined into every usage and optimized away.
259 */
260extern void
261lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg, const char* symbol);
262
Nick Kledzik77595fc2008-02-26 20:26:43 +0000263/**
264 * Writes a new object file at the specified path that contains the
265 * merged contents of all modules added so far.
266 * Returns true on error (check lto_get_error_message() for details).
267 */
268extern bool
269lto_codegen_write_merged_modules(lto_code_gen_t cg, const char* path);
270
Nick Kledzik77595fc2008-02-26 20:26:43 +0000271/**
272 * Generates code for all added modules into one native object file.
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000273 * On success returns a pointer to a generated mach-o/ELF buffer and
Bill Wendling168f1422012-03-31 10:44:20 +0000274 * length set to the buffer size. The buffer is owned by the
Nick Kledzikef194ed2008-02-27 22:25:36 +0000275 * lto_code_gen_t and will be freed when lto_codegen_dispose()
276 * is called, or lto_codegen_compile() is called again.
Nick Kledzik77595fc2008-02-26 20:26:43 +0000277 * On failure, returns NULL (check lto_get_error_message() for details).
278 */
Nick Kledzikef194ed2008-02-27 22:25:36 +0000279extern const void*
Nick Kledzik77595fc2008-02-26 20:26:43 +0000280lto_codegen_compile(lto_code_gen_t cg, size_t* length);
281
Rafael Espindola6421a882011-03-22 20:57:13 +0000282/**
283 * Generates code for all added modules into one native object file.
284 * The name of the file is written to name. Returns true on error.
285 */
286extern bool
287lto_codegen_compile_to_file(lto_code_gen_t cg, const char** name);
288
Nick Kledzik77595fc2008-02-26 20:26:43 +0000289
Devang Patela93ae712008-07-03 22:53:14 +0000290/**
291 * Sets options to help debug codegen bugs.
292 */
293extern void
294lto_codegen_debug_options(lto_code_gen_t cg, const char *);
Bill Wendling168f1422012-03-31 10:44:20 +0000295
Benjamin Kramer8a2ce5d2012-11-24 16:59:10 +0000296/**
297 * Initializes LLVM disassemblers.
298 * FIXME: This doesn't really belong here.
299 */
300extern void
301lto_initialize_disassembler(void);
302
Nick Kledzik77595fc2008-02-26 20:26:43 +0000303#ifdef __cplusplus
304}
305#endif
306
Gregory Szorc6244b512012-03-21 03:54:29 +0000307/**
308 * @}
309 */
Nick Kledzik77595fc2008-02-26 20:26:43 +0000310
311#endif