GithubHelp home page GithubHelp logo

cloudxtreme / macgyver Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jessfraz/macgyver

0.0 1.0 0.0 164 KB

The Macgyver of Dlopening

License: MIT License

Makefile 34.63% Go 39.50% C 25.87%

macgyver's Introduction

The Macgyver of Dlopening

You can dlopen self. Yes, you heard that right. This repo is showing how this works with regard to cgo and compiling binaries with symbols exported dynamically so that they can dlopen themselves.

Let's try it out

Okay so ls src/ and you can see we have 2 C files, sqrt.c and helloworld.c. These are the functions we will call the symbols for in our main.go.

Running make in the directory creates our object files for the C code that we will link into our go binary.

In main.go the most important thing to note is:

int *Run (const char *fun, const char *arg)
{
    // Passing NULL to dlopen will dlopen self.
    void *hndl = dlopen (NULL, RTLD_LAZY);
    if (!hndl)
    {
        fprintf(stderr, "dlopen failed: %s\n", dlerror());
        return (void *)EXIT_FAILURE;
    }
...
}

This is really the heart of the Macgyver technique ;)

Ok so enough about the code, it's a really small amount of code so I'm sure you can tell right away what is happening.

Let's compile it

$ make

# OR to compile in a docker container
$ make docker

Now we have a binary in our current directory named macgyver.

Check that our symbols from our src/*.c files have been exported.

We pass -D to nm for dynamic exports, and -C for name de-mangling.

$  nm macgyver -DC | grep hello
0000000000454100 T hello

$ nm macgyver -DC | grep squareroot
0000000000454125 T squareroot

This works because we added -Wl,--export-dynamic to our -extldflags for go build. The -Wl,--whole-archive flag makes sure that all our symbols get exported into the final binary, because typically gcc (or any compiler) will only add in the things we need, but since they aren't getting called directly from our code, the compilier does not know we need them.

Let's run it

$ ./macgyver
Hello world, jessie!
Square root of 16 is 4.000000

Yay it worked so in our main.go we ran:

func run(fun, arg string) {
    f := C.CString(fun)
    a := C.CString(arg)
    defer C.free(unsafe.Pointer(f))
    defer C.free(unsafe.Pointer(a))
    C.Run(f, a)
}

func main() {
    run("hello", "jessie")
    run("squareroot", "16")
}

which passed hello and jessie to our helloworld.c module and squareroot and 16 to our sqrt.cmodule. Both modules had their symbols added to our final binary.

If you are more curious checkout the dlopen man page, linux.die.net/man/3/dlopen and gcc link options gcc.gnu.org/onlinedocs/gcc/Link-Options.html.

macgyver's People

Contributors

jessfraz 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.