blob: 2ce98d7f9ca9c60b20bf24cd5e30094aef82b86e [file] [log] [blame]
Eric Christophere243fd92011-04-03 22:34:07 +00001//===- Object.cpp - C bindings to the object file library--------*- 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 file defines the C bindings to the file-format-independent object
11// library.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Object/ObjectFile.h"
16#include "llvm-c/Object.h"
17
18using namespace llvm;
19using namespace object;
20
Owen Anderson3cb05672011-10-21 17:50:59 +000021// ObjectFile creation
Eric Christophere243fd92011-04-03 22:34:07 +000022LLVMObjectFileRef LLVMCreateObjectFile(LLVMMemoryBufferRef MemBuf) {
23 return wrap(ObjectFile::createObjectFile(unwrap(MemBuf)));
24}
25
26void LLVMDisposeObjectFile(LLVMObjectFileRef ObjectFile) {
27 delete unwrap(ObjectFile);
28}
29
Owen Anderson3cb05672011-10-21 17:50:59 +000030// ObjectFile Section iterators
Eric Christophere243fd92011-04-03 22:34:07 +000031LLVMSectionIteratorRef LLVMGetSections(LLVMObjectFileRef ObjectFile) {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +000032 section_iterator SI = unwrap(ObjectFile)->begin_sections();
33 return wrap(new section_iterator(SI));
Eric Christophere243fd92011-04-03 22:34:07 +000034}
35
36void LLVMDisposeSectionIterator(LLVMSectionIteratorRef SI) {
37 delete unwrap(SI);
38}
39
40LLVMBool LLVMIsSectionIteratorAtEnd(LLVMObjectFileRef ObjectFile,
41 LLVMSectionIteratorRef SI) {
42 return (*unwrap(SI) == unwrap(ObjectFile)->end_sections()) ? 1 : 0;
43}
44
45void LLVMMoveToNextSection(LLVMSectionIteratorRef SI) {
Michael J. Spencer25b15772011-06-25 17:55:23 +000046 error_code ec;
47 unwrap(SI)->increment(ec);
48 if (ec) report_fatal_error("LLVMMoveToNextSection failed: " + ec.message());
Eric Christophere243fd92011-04-03 22:34:07 +000049}
50
Owen Anderson3cb05672011-10-21 17:50:59 +000051// ObjectFile Symbol iterators
52LLVMSymbolIteratorRef LLVMGetSymbols(LLVMObjectFileRef ObjectFile) {
53 symbol_iterator SI = unwrap(ObjectFile)->begin_symbols();
54 return wrap(new symbol_iterator(SI));
55}
56
57void LLVMDisposeSymbolIterator(LLVMSymbolIteratorRef SI) {
58 delete unwrap(SI);
59}
60
61LLVMBool LLVMIsSymbolIteratorAtEnd(LLVMObjectFileRef ObjectFile,
62 LLVMSymbolIteratorRef SI) {
63 return (*unwrap(SI) == unwrap(ObjectFile)->end_symbols()) ? 1 : 0;
64}
65
66void LLVMMoveToNextSymbol(LLVMSymbolIteratorRef SI) {
67 error_code ec;
68 unwrap(SI)->increment(ec);
69 if (ec) report_fatal_error("LLVMMoveToNextSymbol failed: " + ec.message());
70}
71
72// SectionRef accessors
Eric Christophere243fd92011-04-03 22:34:07 +000073const char *LLVMGetSectionName(LLVMSectionIteratorRef SI) {
Michael J. Spencer25b15772011-06-25 17:55:23 +000074 StringRef ret;
75 if (error_code ec = (*unwrap(SI))->getName(ret))
76 report_fatal_error(ec.message());
77 return ret.data();
Eric Christophere243fd92011-04-03 22:34:07 +000078}
79
80uint64_t LLVMGetSectionSize(LLVMSectionIteratorRef SI) {
Michael J. Spencer25b15772011-06-25 17:55:23 +000081 uint64_t ret;
82 if (error_code ec = (*unwrap(SI))->getSize(ret))
83 report_fatal_error(ec.message());
84 return ret;
Eric Christophere243fd92011-04-03 22:34:07 +000085}
86
87const char *LLVMGetSectionContents(LLVMSectionIteratorRef SI) {
Michael J. Spencer25b15772011-06-25 17:55:23 +000088 StringRef ret;
89 if (error_code ec = (*unwrap(SI))->getContents(ret))
90 report_fatal_error(ec.message());
91 return ret.data();
Eric Christophere243fd92011-04-03 22:34:07 +000092}
Owen Anderson3cb05672011-10-21 17:50:59 +000093
94uint64_t LLVMGetSectionAddress(LLVMSectionIteratorRef SI) {
95 uint64_t ret;
96 if (error_code ec = (*unwrap(SI))->getAddress(ret))
97 report_fatal_error(ec.message());
98 return ret;
99}
100
101int LLVMGetSectionContainsSymbol(LLVMSectionIteratorRef SI,
102 LLVMSymbolIteratorRef Sym) {
103 bool ret;
104 if (error_code ec = (*unwrap(SI))->containsSymbol(**unwrap(Sym), ret))
105 report_fatal_error(ec.message());
106 return ret;
107}
108
109// SymbolRef accessors
110const char *LLVMGetSymbolName(LLVMSymbolIteratorRef SI) {
111 StringRef ret;
112 if (error_code ec = (*unwrap(SI))->getName(ret))
113 report_fatal_error(ec.message());
114 return ret.data();
115}
116
117uint64_t LLVMGetSymbolAddress(LLVMSymbolIteratorRef SI) {
118 uint64_t ret;
119 if (error_code ec = (*unwrap(SI))->getAddress(ret))
120 report_fatal_error(ec.message());
121 return ret;
122}
123
124uint64_t LLVMGetSymbolOffset(LLVMSymbolIteratorRef SI) {
125 uint64_t ret;
126 if (error_code ec = (*unwrap(SI))->getOffset(ret))
127 report_fatal_error(ec.message());
128 return ret;
129}
130
131uint64_t LLVMGetSymbolSize(LLVMSymbolIteratorRef SI) {
132 uint64_t ret;
133 if (error_code ec = (*unwrap(SI))->getSize(ret))
134 report_fatal_error(ec.message());
135 return ret;
136}
137