1193323Sed/*===-- llvm-c/lto.h - LTO Public C Interface ---------------------*- C -*-===*\ 2193323Sed|* *| 3193323Sed|* The LLVM Compiler Infrastructure *| 4193323Sed|* *| 5193323Sed|* This file is distributed under the University of Illinois Open Source *| 6193323Sed|* License. See LICENSE.TXT for details. *| 7193323Sed|* *| 8193323Sed|*===----------------------------------------------------------------------===*| 9193323Sed|* *| 10193323Sed|* This header provides public interface to an abstract link time optimization*| 11193323Sed|* library. LLVM provides an implementation of this interface for use with *| 12193323Sed|* llvm bitcode files. *| 13193323Sed|* *| 14193323Sed\*===----------------------------------------------------------------------===*/ 15193323Sed 16249423Sdim#ifndef LLVM_C_LTO_H 17249423Sdim#define LLVM_C_LTO_H 18193323Sed 19193323Sed#include <stddef.h> 20263508Sdim#include <sys/types.h> 21193323Sed 22263508Sdim#ifndef __cplusplus 23263508Sdim#if !defined(_MSC_VER) 24263508Sdim#include <stdbool.h> 25263508Sdimtypedef bool lto_bool_t; 26263508Sdim#else 27263508Sdim/* MSVC in particular does not have anything like _Bool or bool in C, but we can 28263508Sdim at least make sure the type is the same size. The implementation side will 29263508Sdim use C++ bool. */ 30263508Sdimtypedef unsigned char lto_bool_t; 31263508Sdim#endif 32263508Sdim#else 33263508Sdimtypedef bool lto_bool_t; 34263508Sdim#endif 35263508Sdim 36234353Sdim/** 37234353Sdim * @defgroup LLVMCLTO LTO 38234353Sdim * @ingroup LLVMC 39234353Sdim * 40234353Sdim * @{ 41234353Sdim */ 42234353Sdim 43263508Sdim#define LTO_API_VERSION 5 44193574Sed 45193323Sedtypedef enum { 46218893Sdim LTO_SYMBOL_ALIGNMENT_MASK = 0x0000001F, /* log2 of alignment */ 47234353Sdim LTO_SYMBOL_PERMISSIONS_MASK = 0x000000E0, 48234353Sdim LTO_SYMBOL_PERMISSIONS_CODE = 0x000000A0, 49234353Sdim LTO_SYMBOL_PERMISSIONS_DATA = 0x000000C0, 50234353Sdim LTO_SYMBOL_PERMISSIONS_RODATA = 0x00000080, 51234353Sdim LTO_SYMBOL_DEFINITION_MASK = 0x00000700, 52234353Sdim LTO_SYMBOL_DEFINITION_REGULAR = 0x00000100, 53234353Sdim LTO_SYMBOL_DEFINITION_TENTATIVE = 0x00000200, 54234353Sdim LTO_SYMBOL_DEFINITION_WEAK = 0x00000300, 55234353Sdim LTO_SYMBOL_DEFINITION_UNDEFINED = 0x00000400, 56218893Sdim LTO_SYMBOL_DEFINITION_WEAKUNDEF = 0x00000500, 57234353Sdim LTO_SYMBOL_SCOPE_MASK = 0x00003800, 58234353Sdim LTO_SYMBOL_SCOPE_INTERNAL = 0x00000800, 59234353Sdim LTO_SYMBOL_SCOPE_HIDDEN = 0x00001000, 60234353Sdim LTO_SYMBOL_SCOPE_PROTECTED = 0x00002000, 61218893Sdim LTO_SYMBOL_SCOPE_DEFAULT = 0x00001800, 62218893Sdim LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN = 0x00002800 63193323Sed} lto_symbol_attributes; 64193323Sed 65193323Sedtypedef enum { 66193323Sed LTO_DEBUG_MODEL_NONE = 0, 67193323Sed LTO_DEBUG_MODEL_DWARF = 1 68193323Sed} lto_debug_model; 69193323Sed 70193323Sedtypedef enum { 71193323Sed LTO_CODEGEN_PIC_MODEL_STATIC = 0, 72193323Sed LTO_CODEGEN_PIC_MODEL_DYNAMIC = 1, 73193323Sed LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC = 2 74193323Sed} lto_codegen_model; 75193323Sed 76193323Sed 77193323Sed/** opaque reference to a loaded object module */ 78193323Sedtypedef struct LTOModule* lto_module_t; 79193323Sed 80193323Sed/** opaque reference to a code generator */ 81193323Sedtypedef struct LTOCodeGenerator* lto_code_gen_t; 82193323Sed 83193323Sed#ifdef __cplusplus 84193323Sedextern "C" { 85193323Sed#endif 86193323Sed 87193323Sed/** 88193323Sed * Returns a printable string. 89193323Sed */ 90193323Sedextern const char* 91193323Sedlto_get_version(void); 92193323Sed 93193323Sed 94193323Sed/** 95221345Sdim * Returns the last error string or NULL if last operation was successful. 96193323Sed */ 97193323Sedextern const char* 98193323Sedlto_get_error_message(void); 99193323Sed 100193323Sed/** 101193323Sed * Checks if a file is a loadable object file. 102193323Sed */ 103263508Sdimextern lto_bool_t 104193323Sedlto_module_is_object_file(const char* path); 105193323Sed 106193323Sed 107193323Sed/** 108193323Sed * Checks if a file is a loadable object compiled for requested target. 109193323Sed */ 110263508Sdimextern lto_bool_t 111234353Sdimlto_module_is_object_file_for_target(const char* path, 112193323Sed const char* target_triple_prefix); 113193323Sed 114193323Sed 115193323Sed/** 116193323Sed * Checks if a buffer is a loadable object file. 117193323Sed */ 118263508Sdimextern lto_bool_t 119193323Sedlto_module_is_object_file_in_memory(const void* mem, size_t length); 120193323Sed 121193323Sed 122193323Sed/** 123193323Sed * Checks if a buffer is a loadable object compiled for requested target. 124193323Sed */ 125263508Sdimextern lto_bool_t 126234353Sdimlto_module_is_object_file_in_memory_for_target(const void* mem, size_t length, 127210299Sed const char* target_triple_prefix); 128193323Sed 129193323Sed 130193323Sed/** 131193323Sed * Loads an object file from disk. 132193323Sed * Returns NULL on error (check lto_get_error_message() for details). 133193323Sed */ 134193323Sedextern lto_module_t 135193323Sedlto_module_create(const char* path); 136193323Sed 137193323Sed 138193323Sed/** 139193323Sed * Loads an object file from memory. 140193323Sed * Returns NULL on error (check lto_get_error_message() for details). 141193323Sed */ 142193323Sedextern lto_module_t 143193323Sedlto_module_create_from_memory(const void* mem, size_t length); 144193323Sed 145218893Sdim/** 146218893Sdim * Loads an object file from disk. The seek point of fd is not preserved. 147218893Sdim * Returns NULL on error (check lto_get_error_message() for details). 148218893Sdim */ 149218893Sdimextern lto_module_t 150221345Sdimlto_module_create_from_fd(int fd, const char *path, size_t file_size); 151193323Sed 152221345Sdim/** 153221345Sdim * Loads an object file from disk. The seek point of fd is not preserved. 154221345Sdim * Returns NULL on error (check lto_get_error_message() for details). 155221345Sdim */ 156221345Sdimextern lto_module_t 157221345Sdimlto_module_create_from_fd_at_offset(int fd, const char *path, size_t file_size, 158221345Sdim size_t map_size, off_t offset); 159218893Sdim 160221345Sdim 161193323Sed/** 162193323Sed * Frees all memory internally allocated by the module. 163193323Sed * Upon return the lto_module_t is no longer valid. 164193323Sed */ 165193323Sedextern void 166193323Sedlto_module_dispose(lto_module_t mod); 167193323Sed 168193323Sed 169193323Sed/** 170193323Sed * Returns triple string which the object module was compiled under. 171193323Sed */ 172193323Sedextern const char* 173193323Sedlto_module_get_target_triple(lto_module_t mod); 174193323Sed 175212904Sdim/** 176212904Sdim * Sets triple string with which the object will be codegened. 177212904Sdim */ 178212904Sdimextern void 179212904Sdimlto_module_set_target_triple(lto_module_t mod, const char *triple); 180193323Sed 181212904Sdim 182193323Sed/** 183193323Sed * Returns the number of symbols in the object module. 184193323Sed */ 185218893Sdimextern unsigned int 186193323Sedlto_module_get_num_symbols(lto_module_t mod); 187193323Sed 188193323Sed 189193323Sed/** 190193323Sed * Returns the name of the ith symbol in the object module. 191193323Sed */ 192193323Sedextern const char* 193218893Sdimlto_module_get_symbol_name(lto_module_t mod, unsigned int index); 194193323Sed 195193323Sed 196193323Sed/** 197193323Sed * Returns the attributes of the ith symbol in the object module. 198193323Sed */ 199193323Sedextern lto_symbol_attributes 200218893Sdimlto_module_get_symbol_attribute(lto_module_t mod, unsigned int index); 201193323Sed 202193323Sed 203193323Sed/** 204193323Sed * Instantiates a code generator. 205193323Sed * Returns NULL on error (check lto_get_error_message() for details). 206193323Sed */ 207193323Sedextern lto_code_gen_t 208193323Sedlto_codegen_create(void); 209193323Sed 210193323Sed 211193323Sed/** 212193323Sed * Frees all code generator and all memory it internally allocated. 213193323Sed * Upon return the lto_code_gen_t is no longer valid. 214193323Sed */ 215193323Sedextern void 216193323Sedlto_codegen_dispose(lto_code_gen_t); 217193323Sed 218193323Sed 219193323Sed 220193323Sed/** 221193323Sed * Add an object module to the set of modules for which code will be generated. 222193323Sed * Returns true on error (check lto_get_error_message() for details). 223193323Sed */ 224263508Sdimextern lto_bool_t 225193323Sedlto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod); 226193323Sed 227193323Sed 228193323Sed 229193323Sed/** 230193323Sed * Sets if debug info should be generated. 231193323Sed * Returns true on error (check lto_get_error_message() for details). 232193323Sed */ 233263508Sdimextern lto_bool_t 234193323Sedlto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model); 235193323Sed 236193323Sed 237193323Sed/** 238193323Sed * Sets which PIC code model to generated. 239193323Sed * Returns true on error (check lto_get_error_message() for details). 240193323Sed */ 241263508Sdimextern lto_bool_t 242193323Sedlto_codegen_set_pic_model(lto_code_gen_t cg, lto_codegen_model); 243193323Sed 244193323Sed 245193323Sed/** 246212904Sdim * Sets the cpu to generate code for. 247193323Sed */ 248193323Sedextern void 249212904Sdimlto_codegen_set_cpu(lto_code_gen_t cg, const char *cpu); 250193323Sed 251193323Sed 252193323Sed/** 253193574Sed * Sets the location of the assembler tool to run. If not set, libLTO 254193574Sed * will use gcc to invoke the assembler. 255193574Sed */ 256193574Sedextern void 257193574Sedlto_codegen_set_assembler_path(lto_code_gen_t cg, const char* path); 258193574Sed 259212904Sdim/** 260212904Sdim * Sets extra arguments that libLTO should pass to the assembler. 261212904Sdim */ 262212904Sdimextern void 263212904Sdimlto_codegen_set_assembler_args(lto_code_gen_t cg, const char **args, 264212904Sdim int nargs); 265193574Sed 266193574Sed/** 267263508Sdim * Tells LTO optimization passes that this symbol must be preserved 268263508Sdim * because it is referenced by native code or a command line option. 269193323Sed */ 270193323Sedextern void 271193323Sedlto_codegen_add_must_preserve_symbol(lto_code_gen_t cg, const char* symbol); 272193323Sed 273193323Sed/** 274193323Sed * Writes a new object file at the specified path that contains the 275193323Sed * merged contents of all modules added so far. 276193323Sed * Returns true on error (check lto_get_error_message() for details). 277193323Sed */ 278263508Sdimextern lto_bool_t 279193323Sedlto_codegen_write_merged_modules(lto_code_gen_t cg, const char* path); 280193323Sed 281193323Sed/** 282193323Sed * Generates code for all added modules into one native object file. 283221345Sdim * On success returns a pointer to a generated mach-o/ELF buffer and 284234353Sdim * length set to the buffer size. The buffer is owned by the 285193323Sed * lto_code_gen_t and will be freed when lto_codegen_dispose() 286193323Sed * is called, or lto_codegen_compile() is called again. 287193323Sed * On failure, returns NULL (check lto_get_error_message() for details). 288193323Sed */ 289193323Sedextern const void* 290193323Sedlto_codegen_compile(lto_code_gen_t cg, size_t* length); 291193323Sed 292221345Sdim/** 293221345Sdim * Generates code for all added modules into one native object file. 294221345Sdim * The name of the file is written to name. Returns true on error. 295221345Sdim */ 296263508Sdimextern lto_bool_t 297221345Sdimlto_codegen_compile_to_file(lto_code_gen_t cg, const char** name); 298193323Sed 299221345Sdim 300193323Sed/** 301193323Sed * Sets options to help debug codegen bugs. 302193323Sed */ 303193323Sedextern void 304193323Sedlto_codegen_debug_options(lto_code_gen_t cg, const char *); 305234353Sdim 306249423Sdim/** 307249423Sdim * Initializes LLVM disassemblers. 308249423Sdim * FIXME: This doesn't really belong here. 309249423Sdim */ 310249423Sdimextern void 311249423Sdimlto_initialize_disassembler(void); 312249423Sdim 313193323Sed#ifdef __cplusplus 314193323Sed} 315193323Sed#endif 316193323Sed 317234353Sdim/** 318234353Sdim * @} 319234353Sdim */ 320193323Sed 321193323Sed#endif 322