Shared Library

Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
or

Already have an account? Sign In »

Course
Time
13 hours 15 minutes
Difficulty
Beginner
Video Transcription
00:03
Hello. This is Dr Miller and this is Episode 15.6 of Assembly.
00:09
Today we're gonna talk about a shared library in building it,
00:12
so creating a shared library.
00:15
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.
00:22
So a couple of them are the minus f pic. And so this will create our position Independent code.
00:27
And then we also want to use the minus shared.
00:30
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,
00:39
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.
00:58
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.
01:15
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.
01:30
So here we have our make file.
01:33
So if we want to, we'll start at the bottom, and we'll sort of work our way up.
01:37
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,
01:47
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.
01:56
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,
02:06
So now we want to convert that object file into a shared library.
02:10
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,
02:19
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
02:36
and then when we want to build lib A when we want to build the shared library. So our main, um, program
02:45
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,
03:05
and then we'll creator output file, which is main dot out
03:07
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.
03:22
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
03:35
it will end up in the PLT. And they got so in this example, Um,
03:44
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
04:02
and so this dot end label.
04:05
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.
04:15
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.
04:29
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
04:45
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
04:56
and so that will make sure
04:57
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
05:05
references to A s and main
05:09
in maine dot c. We can for a s a main weaken. Basically tell it that there exists a function called s a main,
05:16
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.
05:30
And so we're able to call A s a main and then we're able to reference hello and hello to
05:38
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.
05:46
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,
05:56
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
06:15
are going to be in the plt.
06:16
And so when we do the call to those, they're referencing the PLT and not just the normal library function.
06:25
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
06:45
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.
06:56
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,
07:09
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.
07:30
So today we talked about creating a shared library,
07:33
and in the future, we're gonna talk about we'll do an actual example of creating a shared library.
07:40
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.
Up Next