Problems throwing and catching exceptions on OS X with -fno-rtti

后端 未结 3 1054
梦如初夏
梦如初夏 2020-12-31 04:15

The issue is somewhat similar to this question but the accepted answer does not really propose a solution or workaround.

In our project, we have a dylib and the main

3条回答
  •  独厮守ぢ
    2020-12-31 04:21

    Well, even though I have accepted an answer it did not solve all problems. So I'm writing down the solution which did work in the end.

    I made a small tool for post-processing the object files and marking the local symbols as UNDEF. This forces the linker to use definitions from libstdc++ and not local ones from the file. The basic approach of the tool is:

    1. load the Mach-O header
    2. walk the load commands and find the LC_SYMTAB command
    3. load the list of symbols (struct nlist) and the strings
    4. walk the symbols and look for those that we need (e.g. __ZTISt9bad_alloc)
    5. set the found symbols' type to N_UNDF|N_EXT.
    6. after processing, write the modified symbol table back to the file.

    (I also made a similar implementation for ELF)

    I post-process any file that's using std exceptions, either for throwing or for catching. To make sure the file list does not go stale, I added a post-link check for unwanted local symbols using nm.

    This seems to resolve all the problems I've had so far.

提交回复
热议问题