Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
Already have an account? Sign In »
13 hours 15 minutes
Hello. This is Dr Miller and this is Episode 15.6 of Assembly.
Today we're gonna talk about a shared library in building it,
so creating a shared library.
So some of the things that we need to do when we're creating a shared library is we need to use some additional flags to GCC.
So a couple of them are the minus f pic. And so this will create our position Independent code.
And then we also want to use the minus shared.
And so, typically, when you create a shared library, the name is going to end with dot eso, and so we'll use that in there as well,
and then a couple others that we want to do. So we need to be able to find the library, so there's gonna be two steps in creating it and using it. So one is that you have to create it. And then you basically have to tell GCC where it needs to look for the shared library in which shared libraries that you wanted to link to.
And so we got the include path, So search for headers. We have the minus. L So what directory? We're gonna search for shared code in and then you're going to say what library you want to link against. And so, in this example, it says minus L. A.
And so, by default, it's going to assume that you're looking for a library that starts with the words L I be the name of the library you have dot eso. And so if I said minus l Fred, then it would be looking for lib Fred S O.
So here we have our make file.
So if we want to, we'll start at the bottom, and we'll sort of work our way up.
So for the Iot library, we're going to build it using the minus f flag, and we're going to create an elf 64 bit. And our output file is ioo 640.0,
now for our shared library, I'm gonna call this lib a 0.0, now it's in a file called shared dot assam. But it doesn't matter, right? I can create any output that I want.
So again, Nazem minus f elf 64 this is my input file. And then this is the file that's going to be created, so live a 640.0,
So now we want to convert that object file into a shared library.
And so we're going to create lib a dog s so it's going to require both live Ada O and I Oda. Oh,
and so when we build it, we say GCC minus f pic. So we want to create position. Independent code for a shared library are output file. So that should match is live a dot s o And then here, the to object files that we're going to include when we build that
and then when we want to build lib A when we want to build the shared library. So our main, um, program
again, we have to tell it to look in the current directory for both header files. And also you can look in the current directory for the library file and then the ordering of this is important. We want toe link with lib a right, and so this is live Ada eso. So we went toe link with that,
and then we'll creator output file, which is main dot out
and so that will make sure that when main dot c gets compiled and this output is created, it's linked, saying I need to use the library called Libby dot s o. And it does that an appropriate fashion.
So then when we're creating our shared library, we have to include things like the fact that we want to export this symbol hello to and it's gonna end up in the PLT. And so and
it will end up in the PLT. And they got so in this example, Um,
So in this example, we have shared dot sm, and we're going to tell the library that we want to use thes global variables. So we have hello to and what we're saying is that this is a piece of data on. We also have to give it a size. And so one of the things that you can say is hello dot end minus hello to
and so this dot end label.
Well, tell us to basically figure out what the differences and that that will tell us how many bites this is. So this is a cue word, so it should be a bites.
Here's another symbol that we're going to export. So we say global. Hello? We're telling it that this is also data and then it's four bites. So again you can hard code it if you'd like. And here I did a rez de on that variable Hello.
And then when we're inside of our main function again, we want to be able to use this. So we have to tell it that s in Maine is not only global, but it's also a function, and that will make it so that it can be exported. And then if you want to do any
operations related to both hello and hello to because they're in a shared library, we have to say with reference to the symbol table for those
and so that will make sure
that, um it uses the peel tea and they got in order to resolve those libraries. And then inside the pl team that got will have
references to A s and main
in maine dot c. We can for a s a main weaken. Basically tell it that there exists a function called s a main,
um, And then, for our variables, we used the X turn keyword in C to tell it this this exists somewhere else, not in this library. And we're gonna end up having those external variables are gonna be stored in our shared library.
And so we're able to call A s a main and then we're able to reference hello and hello to
So this is the disassembly that we see inside of Maine and we can see inside of here that we have a relative link. It says rehl, Hello.
Ah, which may not make a lot of sense until you look at what the actual binary is. And so here we see that these are referencing the rip,
and so you'll notice that these are going to reference a couple of different addresses so each one of these variables will live at a different address. Um, and those will also be modified because rip will change as it goes to this program. You can also see that both that all s a main print f
are going to be in the plt.
And so when we do the call to those, they're referencing the PLT and not just the normal library function.
So then when we try and run our program so dot slash main dot out it will basically say it can't find lib a dot s so and So in order to fix that, you can basically export the LD library path to tell the processor or the shell where to look for that. And so basically, here we're a pending dot, slash the current
directory, and then any other library pass that were defined. So this is an environment variable. And so were dis resolving the old path and pre pending dot slash on that.
So now when we run dot slash may not out, we end up with, um, getting those variables that we had, and we can see that we changed them a little bit to show the A s m. Main got called,
and then we go through and read the shared library. Um, we can look at the and I crop this year so we could see it. That dynamic symbols. We can see that these get exported. So hello to a s and main and hello all get exported as symbol so that the dynamic linker can look up those memory addresses.
So today we talked about creating a shared library,
and in the future, we're gonna talk about we'll do an actual example of creating a shared library.
If you have questions. You can email me, Miller MJ at you and Kate. I e d u. And you can find me on Twitter at Milhouse 30.