LLVMGetFirstFunction() / LLVMGetNextFunction( ) problem

Hi, I have a problem - looking for advice.

I have a source code file with two functions which are compiled into a .bc file.

When the bitcode file is loaded, I can dump the module and see the two functions:


; Materializable
; Function Attrs: noinline nounwind uwtable
define void @matmul(double*, double*, double*, i32, i32, i32) #0 {}

; Materializable
; Function Attrs: noinline nounwind uwtable
define i32 @main() #0 {}

However, when I use LLVMGetirstFunction() and LLVMGetNextFunction() to iterate through the functions in the module, it only iterates through the function “matmul”.

It never finds main().

50 first_func = LLVMGetFirstFunction(module);
(gdb) next
151 last_func = LLVMGetLastFunction(module);
(gdb)
154 for (func = first_func; func != last_func; func = LLVMGetNextFunction(func)) {
(gdb)
155 func_name = LLVMGetValueName(func);
(gdb)
156 func_addr = LLVMGetFunctionAddress(engine, func_name);
(gdb) print func_name
$9 = 0x1efe180 “matmul”
(gdb) next
159 if (!func_addr) continue;
(gdb)
161 symbol_table_add(symbol_table, func_name, func_addr);
(gdb)
164 if ((size = mem_region_lookup(mem_region_anchor, func_addr))) { symbol_table_add(symbol_table, “dummy_symbol”, func_addr + size); }
(gdb)
154 for (func = first_func; func != last_func; func = LLVMGetNextFunction(func)) {
(gdb)
168 symbol_table_calc_symbol_sizes(symbol_table);

Any suggestions appreciated.

Toshi

The intention of the API is that you iterate until LLVMGetNextFunction returns NULL, not the last function. See their implementations:

LLVMValueRef LLVMGetLastFunction(LLVMModuleRef M) {
Module Mod = unwrap(M);
Module::iterator I = Mod->end();
if (I == Mod->begin())
return nullptr;
return wrap(&
–I);
}

LLVMValueRef LLVMGetNextFunction(LLVMValueRef Fn) {
Function *Func = unwrap(Fn);
Module::iterator I(Func);
if (++I == Func->getParent()->end())
return nullptr;
return wrap(&*I);
}

That works - thanks for the explanation. Much appreciated.

Toshi