The Linux Channel

TUTORIALS :: Dynamic linking without STDLIB in Linux user-space C programming

Written by: Ramin Farajpour, edited by Kiran Kankipati - Published: 05-May-2017

Dynamic linker is the part of an operating system that loads and links the shared libraries needed by an executable.

Dynamic linking in Linux: Now, let us dig into the process of dynamically linked shared libraries in Linux. When users start an application, they're invoking an Executable and Linking Format (ELF) image ( The kernel begins with the process of loading the ELF image into user space virtual memory. The kernel notices an ELF section called .interp, which indicates the dynamic linker to be used /lib/

root@raminfp:~# readelf -l /bin/bash 

Elf file type is EXEC (Executable file)
Entry point 0x420650
There are 9 program headers, starting at offset 64

Program Headers:
  Type           Offset             VirtAddr           PhysAddr
                 FileSiz            MemSiz              Flags  Align
  PHDR           0x0000000000000040 0x0000000000400040 0x0000000000400040
                 0x00000000000001f8 0x00000000000001f8  R E    8
  INTERP         0x0000000000000238 0x0000000000400238 0x0000000000400238
                 0x000000000000001c 0x000000000000001c  R      1
      [Requesting program interpreter: /lib64/]
  LOAD           0x0000000000000000 0x0000000000400000 0x0000000000400000
                 0x00000000000f27d4 0x00000000000f27d4  R E    200000
  LOAD           0x00000000000f3628 0x00000000006f3628 0x00000000006f3628
                 0x000000000000b600 0x00000000000111d0  RW     200000
  DYNAMIC        0x00000000000f5de0 0x00000000006f5de0 0x00000000006f5de0
                 0x00000000000001f0 0x00000000000001f0  RW     8
  NOTE           0x0000000000000254 0x0000000000400254 0x0000000000400254
                 0x0000000000000044 0x0000000000000044  R      4
  GNU_EH_FRAME   0x00000000000d72b0 0x00000000004d72b0 0x00000000004d72b0
                 0x0000000000004094 0x0000000000004094  R      4
  GNU_STACK      0x0000000000000000 0x0000000000000000 0x0000000000000000
                 0x0000000000000000 0x0000000000000000  RW     10
  GNU_RELRO      0x00000000000f3628 0x00000000006f3628 0x00000000006f3628
                 0x00000000000029d8 0x00000000000029d8  R      1

 Section to Segment mapping:
  Segment Sections...
   01     .interp 
   02     .interp .note.ABI-tag .gnu.hash .dynsym .dynstr .gnu.version .gnu.version_r .rela.dyn .rela.plt .init .plt .text .fini .rodata .eh_frame_hdr .eh_frame 
   03     .init_array .fini_array .jcr .dynamic .got .got.plt .data .bss 
   04     .dynamic 
   05     .note.ABI-tag 
   06     .eh_frame_hdr 
   08     .init_array .fini_array .jcr .dynamic .got 

We can find on the Internet many such examples of C source code about dynamic linker. But in this context we going to do Dynamic Linking of sample C user-space program without a standard lib (i.e STDLIB). In one of my earlier articles A Linux system call in C without a standard library I showed how to do a printf() in a sample C code with syscall without STDLIB. Now we can use/refer the same source code(mentioned in my previous article) for writing a dynamic linker.

And here is my GitHub project link in which you can refer assm_syscall.c:

int print(){
    write(1, "hello world\n", 13);
    return 0;

In the assm_syscall.c, we have this custom API intptr write(int fd, void const* data, uintptr nbytes). So based on this we can create assm_syscall.h and to test dytest.c:
Here is the contents of dytest.c:

#include "assm_syscall.h"
int main(void)
    return 0;

And here is the contents of assm_syscall.h:

int print();

And here is the contents of the Makefile:

CONFIG=-nostdlib -fno-unwind-tables -fno-asynchronous-unwind-tables




We can now compile the same as shown below:

root@raminfp:~# make
gcc -s -O2 -nostdlib -fno-unwind-tables -fno-asynchronous-unwind-tables -fPIC -shared -o  assm_syscall.c
gcc -o dytest dytest.c ./
root@raminfp:~# ./dytest
hello world
root@raminfp:~# make clean

As you can see we got the output hello world successfully.

Now we can do a test compile once again with the option make nodyn, i.e without the dynamic linker And by doing so we get the following error.

gcc -s -O2 -nostdlib -fno-unwind-tables -fno-asynchronous-unwind-tables -fPIC -shared -o  assm_syscall.c
gcc -o dytest dytest.c
/tmp/cc4JX5qm.o: In function `main':
dytest.c:(.text+0xa): undefined reference to `print'
collect2: error: ld returned 1 exit status
Makefile:13: recipe for target 'nodyn' failed
make: *** [nodyn] Error 1

Here is my entire GitHub source-code which you can download HERE for reference.

So this is how you can do dynamic linking without STDLIB in Linux user-space C programming.

Featured Video:

* Click the image above to watch this video on Youtube ↗

Suggested Topics:

Network Software Development:
 Tail Drop - by Network Schedulers to Drop Packets in Network Appliances ↗

 UDP sample socket code for Systems and Network software developers ↗

 C code to check valid IP Address (IPv4) - Live Demo and Example ↗

 Bufferbloat in a Networking Device or an Appliance ↗

 iPerf tool - for network channel analysis, bandwidth tests and system & network benchmarking ↗

 Network MTU and Path MTU Discovery PMTUD - WAN and Internet Performance Optimization ↗

Research and Projects:
 Data Profiling and Visual Analysis tool for Deep Learning and AI - VLOG - The Linux Channel ↗

 CYRIL ING√ČNIERIE - CoreFreq Linux CPU monitoring software ↗

 Difference between Intel Xeon Server vs Desktop Core i7 vs Intel Mobile Atom vs ARM Processors ↗

 Research Socket overhead in Linux vs Message Queues and benchmarking ↗

 Tour of my new Home Networking Lab within VirtualBox VM ↗

Linux (user-space), Systems Architecture:
 Linux user-space Atomic Operations via GCC Atomic builtins ↗

 A Linux system call in C without a standard library ↗

 Coding a simple look-up-table in C - without Linked lists and a binary search ↗

 Synchronization in Linux user-space - Architecting multi-process and multi-threads ↗

 Dynamic linking without STDLIB in Linux user-space C programming ↗

 Linux user-space - Shared Memory IPC - Live Demo and Example ↗

 C code to read a /proc file in Linux user-space - Live Demo and Example ↗

 Linux user-space Pthread Synchronization via Mutex - Live Demo and Example ↗

Linux Kernel Software Development:
 Tracking code changes in Linux Kernel Source code ↗

 Kernel customization via make menuconfig - Linux Kernel Compilation (or a Kernel Build) ↗

 How to submit your Linux Kernel Patch ↗

 Tracking Linux Kernel bugs in Bugzilla (or Bugzee as we call it in the Industry) ↗

 KERNEL - The Linux Channel - a High Performance Linux Kernel ↗

 How to upgrade or install a latest current release Linux Kernel version manually in Ubuntu? ↗

 Linux Networking Stack :: RFS: Receive Flow Steering ↗

Linux Kernel Internals (PDFs) - by Ramin Farajpour:
 Linux Kernel Internals :: Linux Kernel Boot Process ↗

Software Development (Programming) Tools:
 SNMP and MIB Browsers ↗

 Kompare GUI patch and diff tool in Linux ↗

 Bluefish IDE - a simple versatile light-weight best editor to edit kernel source files, html files, c source code, text files, SQL schema and so on. ↗

Embedded Projects:
 Using a GPS Sensor ↗

 GPS Geo-tracking system using LinkIt ONE ↗


Join The Linux Channel :: Facebook Group

Join a course:

💎 Linux, Kernel and Network Software Developer: PDF Brochure
💎 Linux, Kernel, Networking and Device Drivers: PDF Brochure
💎 PhD or equivalent (or Post Doctoral) looking for assistance: Details
💎 ... or unlimited life-time mentorship: Details

💗 Help shape the future: Sponsor/Donate

Recommended Topics:
Featured Video:

* Click the image above to watch this video on Youtube ↗

Download: Linux Kernel Booting_1.pdf

Trending Video:

* Click the image above to watch this video on Youtube ↗

TUTORIALS :: Linux Networking Stack :: RFS: Receive Flow Steering:

Linux Kernel Programming - Device Drivers:

Research :: Content Delivery Networks (CDN):
CDN Networks

Recommended Video:

* Click the image above to watch this video on Youtube ↗

The TOFFEE Project - v10.20 :: Updated: 27-Feb-2019 :: © 2019 :: Author: Kiran Kankipati
Your IP: :: Browser: CCBot/2.0 (