How to check where function link

In a C / C ++ open source project, I use gcc-arm-embedded (currently the most recent 4.9-2015-q2 ).

For some reason, I should avoid using some functions, for example, some of stdio et cetera (I don't want to use redirection or half-hunting).

In addition, I use FreeRtos with heap_4.c and had, for example, malloc()redirected directly to the pvPortMalloc()following:

void* malloc(size_t s) {
    return pvPortMalloc(s);
}

Therefore, I do not want to have any parts of the toolchain management code inside my binary.

Now there are some situations like the developer of my team means to use, for example. printf(), which indirectly refers to _malloc_r()(and a few more), and it’s actually quite difficult to figure out what it refers to, and so where to fix it.

(Usage printf()is just an example. In my project, I have a custom printf () implementation that outputs directly to uart without using stdio. But there are other cases, for example, the info demangeling type, ...)

I currently have a situation where my project (which contains about 200 c and C ++ source files) is compiled without a link in _malloc_r()any way - while I am creating with gcc 4.8 .

But when creating gcc 4.9, I see unwanted links to _malloc_ra few more.

, ?

2015-07-20:

  • , : gcc 4.9 _malloc_r .
  • , , .
  • , __gnu_cxx::__snprintf_lite(), iostream, . __gnu_cxx::__snprintf_lite() gcc stl (, __throw_out_of_range_fmt()). (, std::map). iostream , __gnu_cxx::__snprintf_lite(), ( vsnprintf):

    namespace __gnu_cxx {
        int __snprintf_lite(char* buf, size_t bufsize, const char* fmt, va_list ap) {
            return vsnprintf(buf, bufsize, fmt, ap);
        }
    }
    

    , gcc-4.9 (, src/gcc/libstdc++-v3/src/c++11/snprintf_lite.cc).

+4
3

_exit :

/* hello.c */
#include <stdlib.h>
#include <unistd.h>

int main(void)
{
    write(1, "Hello\n", 6);
    _exit(0);
}

:

$ gcc hello.c -static -g

_exit:

$ nm a.out | grep " _exit"
000000000040f760 T _exit

objdump -d -j .text, grep _exit, cut addr2line:

$ objdump -d -j .text a.out | grep 40f760 | cut -c 1-8 | addr2line -e a.out -f
oom
dl-tls.o:?
main
/home/m/hello.c:8
__run_exit_handlers
??:?
??
??:0
_Exit
??:?
_dl_non_dynamic_init
??:?
abort
??:?
do_lookup_x
dl-lookup.o:?
_dl_relocate_object
??:?
_dl_signal_error
??:?
dl_open_worker
dl-open.o:?
_dl_close_worker.part.0
dl-close.o:?
_dl_start_profile
??:?

:

oom, main, __run_exit_handlers,... _exit.

+2

Peraphs malloc.h, undef redefine _malloc_r

:

extern _PTR malloc _PARAMS ((size_t));
#ifdef __CYGWIN__
#undef _malloc_r
#define _malloc_r(r, s) malloc (s)
#else
extern _PTR _malloc_r _PARAMS ((struct _reent *, size_t));
#endif

Hooks-for-Malloc

GNU C malloc, realloc , . , , .

malloc.h.

LD_PRELOAD LD_PRELOAD?

+2

, , , , . ?

() printf:

#define printf FORBIDDEN

int main(int argc, char *argv[]) {
  printf("Test");
}

:

Untitled.cpp:11:3: error: no matching function for call to 'FORBIDDEN'
  printf("Test");
  ^~~~~~
Untitled.cpp:3:16: note: expanded from macro 'printf'
#define printf FORBIDDEN
               ^~~~~~~~~

, . , :

#define printf FORBIDDEN

// this in included file:
void otherfunc() {
  printf("I fail.");
}
// eof included file

int main(int argc, char *argv[]) {
  otherfunc();
}
+2

All Articles