Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redefining functions lin libC

Tags:

c

libc

Assume the following simple C code:

file1.c

#include <stdio.h>

char* gets(char* i){
  return i;
}

which is redefining the libC native function gets.

This compiles fine with gcc file1.c.

My question is howcome the linker isn't complaining about duplicate symbols as this function is also defined in libC itself?

like image 704
Alex Avatar asked Mar 20 '19 01:03

Alex


2 Answers

Because you can override functions from standard libraries in C, check this

like image 90
Nguyen Cong Avatar answered Nov 14 '22 02:11

Nguyen Cong


add option -whole-archive in the link phase, as below:

gcc -c -ofile1.o file1.c
gcc -ofile1 -Wl,--whole-archive -lc file1.o -Wl,--no-whole-archive

result:

file1.o: In function `gets':
file1.c:(.text+0x0): multiple definition of `gets'
file1.o: In function `main':
file1.c:(.text+0x18): undefined reference to `gets@@GLIBC_2.2.5'
collect2: error: ld returned 1 exit status
like image 1
ccxxshow Avatar answered Nov 14 '22 00:11

ccxxshow