GithubHelp home page GithubHelp logo

happy-ferret / dynamic-linktime-load Goto Github PK

View Code? Open in Web Editor NEW

This project forked from hpvb/dynamic-linktime-load

0.0 1.0 0.0 8 KB

Small demo of a method to dynamically load your link-time libarary dependencies on GNU/Linux

Makefile 26.95% C 65.24% Shell 7.81%

dynamic-linktime-load's Introduction

Small demo of loading dependent .so files at runtime

This project shows how to load a .so file at runtime without linking to it at compile time. This allows for a program to have optional runtime dependencies without generating a full stub for the library using dlopen() and then using dlsym() for each used symbol.

We achieve this by exploiting several mechanisms:

  • Weak symbol linkage
  • GCC's #pragma weak
  • GCC's -fpie (Position Independent Executable) flag

We use a readelf to generate a .c file with a #pragma weak for each of the exported symbols of a particular .so file. This means that the software author does not have to keep track of what symbols are in use (such as with the dlsym() approach) nor does a complicated generator have to be written.

Instead we only dlopen() the target library with RTLD_NOW and RTLD_GLOBAL to overwrite the weak (non-existant!) symbols in the position independent executable.

From the demo project:

void* libhello_handle = dlopen("./libhello.so", RTLD_NOW | RTLD_GLOBAL);
if (libhello_handle) {
    puts("We loaded our library");
} else {
    puts("We failed to load our library");
}

To get a feel for how little the programmer has to think about this try adding a function to libhello.c/h and call it from main.c.

Have your PIE and eat it too

Dissassembly comparison:

PIE:

  4006ba:	b8 00 00 00 00       	mov    $0x0,%eax
  4006bf:	e8 cc fe ff ff       	callq  400590 <dynamic_hello_world@plt>

Regular linkage (non-PIE):

  400788:	b8 00 00 00 00       	mov    $0x0,%eax
  40078d:	e8 ce fe ff ff       	callq  400660 <dynamic_hello_world@plt>

dynamic-linktime-load's People

Contributors

hpvb avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.