最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

C++ undefined reference to `typeinfo for int' when throwing exception without the standard library - Stack Overflow

programmeradmin5浏览0评论

For a project I'm working on, I'm writing a mini standard library replacement. For this, I'm currently trying to implement exceptions. So, I wrote the following minimal code

int memory[4];

void* __cxa_allocate_exception(long unsigned int thrown_size) throw()
{
    return memory;
}

void __cxa_free_exception(void * thrown_exception) throw()
{
    
}

void __cxa_throw(void* thrown_exception, void * tinfo, void (*dest)(void*))
{
    while(1){};
}

extern "C" void _start()
{
    throw 2;
}

However, when compiling this with -nostdlib, I get the following error

undefined reference to `typeinfo for int'

One of the big things throwing me off here, is what is 'typeinfo for int'??? It's not the name of a function, variable etc since it has spaces in it. Checking the compiler output in godbolt shows the following

 mov     esi, OFFSET FLAT:typeinfo for int

So I'm completely lost as to what it actually is!

Also, some answers seem to solve this by just including libstdc++ however this does not seem to work for me. I put the above code into godbolt, with the following compiler flags

-nostdlib -frtti -static-libstdc++

However I still get the same error and I'm not sure why. For reference, here is the code on godbolt showing said error.

For a project I'm working on, I'm writing a mini standard library replacement. For this, I'm currently trying to implement exceptions. So, I wrote the following minimal code

int memory[4];

void* __cxa_allocate_exception(long unsigned int thrown_size) throw()
{
    return memory;
}

void __cxa_free_exception(void * thrown_exception) throw()
{
    
}

void __cxa_throw(void* thrown_exception, void * tinfo, void (*dest)(void*))
{
    while(1){};
}

extern "C" void _start()
{
    throw 2;
}

However, when compiling this with -nostdlib, I get the following error

undefined reference to `typeinfo for int'

One of the big things throwing me off here, is what is 'typeinfo for int'??? It's not the name of a function, variable etc since it has spaces in it. Checking the compiler output in godbolt shows the following

 mov     esi, OFFSET FLAT:typeinfo for int

So I'm completely lost as to what it actually is!

Also, some answers seem to solve this by just including libstdc++ however this does not seem to work for me. I put the above code into godbolt, with the following compiler flags

-nostdlib -frtti -static-libstdc++

However I still get the same error and I'm not sure why. For reference, here is the code on godbolt showing said error.

Share Improve this question asked Feb 16 at 23:49 Jade MarkerJade Marker 111 silver badge3 bronze badges New contributor Jade Marker is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 6
  • typeinfo is runtime type info (RTTI) for a given type. It's needed to handle catch - which can catch an exception by reference to base class for example. You will probably need to dig deep into Itanium ABI to actually satisfy the requirements for throwing exceptions without standard library - quick glance suggests that defining std::type_info will be necessary, along with whatever magic is used to generate typeinfo for fundamental types. – Yksisarvinen Commented Feb 17 at 0:18
  • I took a look at the itanium abi, but I struggled to put together how it is actually implemented. It mentions at one point that if you do e.g. throw X;, then it will pass type_info for X to __cxa_throw. I'm assuming that this is where the `typeinfo for int' comes from. But that still begs the question of how is that actually defined? I'm not quite putting together in my head how you can go from some std::type_info to "typeinfo for int" as a definition – Jade Marker Commented Feb 17 at 1:36
  • Look at fundamental_type_info. It says: This has special meaning to the compiler, and will cause it to emit the type_info structures for the fundamental types – kakkoko Commented Feb 17 at 1:48
  • What does “special meaning” mean? The answer is in gcc/cp/rtti. If std::__fundamental_type_info is defined, and its destructor is defined, then the runtime is being built. And then do exactly that: find the name __fundamental_type_info and its destructor definition, and if it finds it, insert the necessary definitions. All of these are hard-coded into the compiler. – kakkoko Commented Feb 17 at 1:56
  • Looks like you might be interested in this cppcon talk : C++ Exceptions for Smaller Firmware - Khalil Estell - CppCon 2024, it does touch the handling exceptions based on types part too – Pepijn Kramer Commented Feb 17 at 3:52
 |  Show 1 more comment

1 Answer 1

Reset to default 1

So this was already answered by kakkoko's comment. But I just wanted to put this into an answer so that the question is marked as solved.

So! Most compilers follow the Itanium abi, meaning that there is a defined interface. Essentially, when type_info and __fundamental_type_info are defined as the abi declares them (so matching the namespace, functions etc), then the compiler works some magic in the background and defines the type info for you!

I tried to put this into godbolt, but it seems it needs them to be defined in a separate file.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论