Expand the coverage of the libObject C bindings to include more SectionRef accessors...
[oota-llvm.git] / lib / Object / Object.cpp
1 //===- 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
18 using namespace llvm;
19 using namespace object;
20
21 // ObjectFile creation
22 LLVMObjectFileRef LLVMCreateObjectFile(LLVMMemoryBufferRef MemBuf) {
23   return wrap(ObjectFile::createObjectFile(unwrap(MemBuf)));
24 }
25
26 void LLVMDisposeObjectFile(LLVMObjectFileRef ObjectFile) {
27   delete unwrap(ObjectFile);
28 }
29
30 // ObjectFile Section iterators
31 LLVMSectionIteratorRef LLVMGetSections(LLVMObjectFileRef ObjectFile) {
32   section_iterator SI = unwrap(ObjectFile)->begin_sections();
33   return wrap(new section_iterator(SI));
34 }
35
36 void LLVMDisposeSectionIterator(LLVMSectionIteratorRef SI) {
37   delete unwrap(SI);
38 }
39
40 LLVMBool LLVMIsSectionIteratorAtEnd(LLVMObjectFileRef ObjectFile,
41                                 LLVMSectionIteratorRef SI) {
42   return (*unwrap(SI) == unwrap(ObjectFile)->end_sections()) ? 1 : 0;
43 }
44
45 void LLVMMoveToNextSection(LLVMSectionIteratorRef SI) {
46   error_code ec;
47   unwrap(SI)->increment(ec);
48   if (ec) report_fatal_error("LLVMMoveToNextSection failed: " + ec.message());
49 }
50
51 // ObjectFile Symbol iterators
52 LLVMSymbolIteratorRef LLVMGetSymbols(LLVMObjectFileRef ObjectFile) {
53   symbol_iterator SI = unwrap(ObjectFile)->begin_symbols();
54   return wrap(new symbol_iterator(SI));
55 }
56
57 void LLVMDisposeSymbolIterator(LLVMSymbolIteratorRef SI) {
58   delete unwrap(SI);
59 }
60
61 LLVMBool LLVMIsSymbolIteratorAtEnd(LLVMObjectFileRef ObjectFile,
62                                 LLVMSymbolIteratorRef SI) {
63   return (*unwrap(SI) == unwrap(ObjectFile)->end_symbols()) ? 1 : 0;
64 }
65
66 void 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
73 const char *LLVMGetSectionName(LLVMSectionIteratorRef SI) {
74   StringRef ret;
75   if (error_code ec = (*unwrap(SI))->getName(ret))
76    report_fatal_error(ec.message());
77   return ret.data();
78 }
79
80 uint64_t LLVMGetSectionSize(LLVMSectionIteratorRef SI) {
81   uint64_t ret;
82   if (error_code ec = (*unwrap(SI))->getSize(ret))
83     report_fatal_error(ec.message());
84   return ret;
85 }
86
87 const char *LLVMGetSectionContents(LLVMSectionIteratorRef SI) {
88   StringRef ret;
89   if (error_code ec = (*unwrap(SI))->getContents(ret))
90     report_fatal_error(ec.message());
91   return ret.data();
92 }
93
94 uint64_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
101 int 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
110 const 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
117 uint64_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
124 uint64_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
131 uint64_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