Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If a standard library function is reimplemented, which of the two is called?

Tags:

c

unix

If a library function like e.g. malloc is reimplemented, then there are two symbols with that name, one in a local object file and one in the system library. Which of the two is used when a function from e.g. stdio is used, which calls malloc (and why)?

like image 371
user3224237 Avatar asked Jun 05 '16 06:06

user3224237


People also ask

Which function is known as standard library function?

C Standard library functions or simply C Library functions are inbuilt functions in C programming. The prototype and data definitions of these functions are present in their respective header files. To use these functions we need to include the header file in our program.

Which function is known as standard library function in C Plus Plus?

iostream. This header contains the prototype for standard input and output functions used in C++ like cin, cout, etc. cmath. This is the header containing various math library functions.

What are the types of functions in C language library functions?

There are two types of functions in C programming: Library Functions: are the functions which are declared in the C header files such as scanf(), printf(), gets(), puts(), ceil(), floor() etc.

How many standard library functions are there in C?

The ANSI C standard library consists of 24 C header files which can be included into a programmer's project with a single directive. Each header file contains one or more function declarations, data type definitions and macros.

What are the library functions?

Library functions are built-in functions that are grouped together and placed in a common location called library. Each function here performs a specific operation. We can use this library functions to get the pre-defined output. All C standard library functions are declared by using many header files.


1 Answers

The link behaviour is, in a general way:

  • Include all symbols defined in the object files.
  • Then resolve the undefined ones using the objects in the libs.

So, if malloc is reimplemented and linked as object file, the version in the object file will override the version in the standard libraries. However, if the new malloc is linked as a library, it depends on the libraries linking order.

Another way, considering the gnu binutils as scope, to override library functions is to wrap the function using the --wrap parameter: https://ftp.gnu.org/old-gnu/Manuals/ld-2.9.1/html_node/ld_3.html

By using the --wrap ld option we can get both functions linked and the wrapper function would be able to call the wrapped one.

The linking order also depends on the command line parameters order. So I'm considering here that libs are listed after objects because, in general, doesn't make sense to put libraries before objects as their objective is to supply the missing symbols demanded by those.

like image 199
olivecoder Avatar answered Oct 24 '22 11:10

olivecoder