PathDB build fixes for PathCC on FreeBSD. #14

Open
wants to merge 17 commits into
from

Conversation

Projects
None yet
2 participants

juikim commented Sep 22, 2011

Now we can build PathDB with PathCC on FreeBSD via path64-suite. Please note it still crashes immediately when it is built as part of Release build. However, I believe it is caused by a compiler bug, which is effectively hidden by '-g' within Debug build. Please see this PR, for example:

https://github.com/path64/compiler/issues/94

juikim added some commits Sep 22, 2011

PathDB must export ps_* symbols for libthread_db.so. However, ld(1) d…
…oes not find it

from libpathdb.a because it is not directly used.  Tell the linker to look at all objects
files in the archive instead.  We may use "--dynamic-list" to limit the exported symbols
later if it is necessary and the linker supports this feature.
Reduce namespace pollution. More specifically, it avoids conflicts wi…
…th getchar(3)

from ANSI C.  Actually, it may be defined as a macro of getc(stdin) in many platforms.

juikim commented Sep 28, 2011

It seems pathCC has an optimizer bug. I re-built PathDB with the following hack and it does not crash any more:

--- src/CMakeLists.txt
+++ src/CMakeLists.txt
@@ -139,6 +139,12 @@
link_directories("${PATHDB_LIBIBERTY_PATH}")
endif()

+# XXX pathCC fails to build a sane binary with any optimization ATM.
+if("${CMAKE_C_COMPILER_ID}" STREQUAL "PathScale" AND "${CMAKE_BUILD_TYPE}" STREQUAL "Release")

  • set(CMAKE_C_FLAGS_RELEASE "-O0")
  • set(CMAKE_CXX_FLAGS_RELEASE "-O0")
    +endif()

add_library(pathdb-lib STATIC ${pathdb_sources})
set_property(TARGET pathdb-lib PROPERTY OUTPUT_NAME "pathdb")
add_library(cli-lib STATIC ${cli_sources})

What about -O1, -O2... I'll help you narrow this down to an exact offending flag.. this will help us save time and get to the bottom of it.. :)

juikim commented Sep 28, 2011

It crashes with any optimization, i.e., -O1, -O2, and -O3. I haven't tried -ipa.

juikim commented Oct 6, 2011

I finally figured out what's happening. It is clearly a bug somewhere in PathCC. When PathDB is compiled with PathCC, it always crashed at the same place, i.e., ELF::get_section() in dbg_elf.cc. First I recompiled everything with -O2, except for dbg_elf.cc, which was compiled with -O0 earlier. Bingo, it didn't crash.


Excerpt from dbg_elf.cc:

BVector ELF::get_section(std::istream & stream, std::string name) {
    for (unsigned int i = 0 ; i < sections.size(); i++) {
        Section *section = sections[i] ;
        if (section->get_name() == name) {
            return section->get_contents(stream) ;
        }
    }
    throw Exception("No such section: %s", name.c_str()) ;
}

Then, I compared assembly outputs from dbg_elf.cc (-O0 vs. -O1), line by line.


-O0 vs. -O1 (both with -g) @ line# 553 - "throw Exception(...);":

        # .aproc        Handler.293.770
        .type   Handler.293.770, @function
-Handler.293.770:       # 0xc7ec
+Handler.293.770:       # 0xb6cc
 .Lt_293_770:
        .loc    1       553     0
-       movq %rax,-152(%rbp)            #  __Exc_Ptr__
-       movq %rdx,-160(%rbp)            #  __Exc_Filter__
-.Lt_293_1026:
-       movq -144(%rbp),%rdi            #  anon426
-       call __cxa_free_exception       #  __cxa_free_exception
+       movq -144(%rbp),%rdi            # [0]  anon426
+       movq %rdx,-160(%rbp)            # [1]  __Exc_Filter__
+       movq %rax,-152(%rbp)            # [1]  __Exc_Ptr__
+       call __cxa_free_exception       # [1]  __cxa_free_exception
 .L_293_6658:
-       movq -152(%rbp),%rdi            #  __Exc_Ptr__
-       movl $0,%eax                    #  
+       xorl %eax,%eax                  # [0]  
+       movq -152(%rbp),%rdi            # [0]  __Exc_Ptr__
        .globl  _Unwind_Resume
-       call _Unwind_Resume             #  _Unwind_Resume
+       call _Unwind_Resume             # [0]  _Unwind_Resume
 .L_293_6914:
-.LBB33__ZN3ELF11get_sectionERSiSs:
-       leave                           #  
-       ret                             #  
 .cfi_endproc
 .LDWend__ZN3ELF11get_sectionERSiSs:
        .size _ZN3ELF11get_sectionERSiSs, .LDWend__ZN3ELF11get_sectionERSiSs-_ZN3ELF11get_sectionERSiSs

As you can see, there are two lines missing, i.e., "leave; ret;". Adding these two lines in the assembly immediately stopped crashes. Please note the same pattern (i.e., missing "leave; ret;" after exception handler) was observed in many places in the same file. Exception handlers are minefield now. :-(

juikim added some commits Oct 14, 2011

Remove misplaced curly-braces and return correct length of the operand.
Simplify nearby case statement with the same style.
Fix register aliases of "rip", "rsp" and "rbp".
This fixes "No such register" exception.  Strangely, this issue does not
manifest itself when it is compiled with GCC 4.2.  Other compilers, e. g.,
Clang, compiled PathDB properly to throw the above exception.

juikim commented Oct 19, 2011

Now PathDB can be built with Clang on FreeBSD and it seems working quite well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment