Hello, this is Dr. Miller and this
is Episode fifteen point six of
Assembly.
Today, we're going to talk about a
shared library and building it
to 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 go.
So a couple of them are the minus
F pick. 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 so.
And so we'll use that in there
as well.
And then a couple of others that we
want to do.
So we need to be able to find the
library. So there's going to 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 Jack where it needs
to look for the
shared library in which shared
libraries that you want it to link
to.
And so we got the include
path. So search for headers.
We have the minus L.
So what directory are you going to
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
library, the name of the library
you have.
So and so if I said
minus Elfriede, then it
would be looking for Leyb, Fred.
So.
So here we have our makefile, so
if we want to, we'll start at the
bottom and we'll sort of work our
way up.
So for the library,
we're going to build it using the
minus F flag and we're going to
create an F sixty four bit
and our output file is
zero.
Now for our shared library, I'm
going to call this Leyb a dot.
Oh, now it's in a file called
Shared ASRM, but it doesn't matter.
Right. I can create any output that
I want.
So again Nazeem minus F
l 64.
This is my input file and then this
is the file that's going to be
created. So Leyb a oh.
So now we want to convert that
object file into a shared
library.
And so we're going to create
Liberdade.
So it's going to require both
Leyb, ADA, O and
O.
And so when we build it we say OK,
minus F PPIC.
So we want to create position,
independent code for a shared
library, our output file.
So that should match is Lib A.D.s.
Oh, and then here are the two
object files that we're going to
include when we build that
and then when we want to build
Leyb A or when we want
to build the shared library.
So our main
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 to link with Leyb
a right.
And so this is Lib Ayda.
So so we want to link
with that and then we'll create our
output file, which is mainboard out.
And so that will make sure that when
Maned C gets compiled
and the output is created,
it's linked, saying I need to
use the library called Livaditis.
So and it does that in 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
going to end up in the.
And so.
And.
It will end up in the pot
and the God, so in this example.
So in this example, we have shared
ASRM and we're going to
tell the library that we want to
use these global variables,
so we have hello
to and what we're saying is
that this is a piece of data and
we also have to give it a size.
And so one of the things that you
can say is hello and minus
hello too.
And so this end
label will tell us to
basically figure out what the
difference is and that that'll tell
us how many bytes this is.
So this is a keyword.
So it should be eight bytes.
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 bytes.
So, again, you can hardcoded, if you
like. And here I did a raised
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 as a man is
not only global, but it's also a
function and that'll 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
it uses the PLT and the got
in order to resolve those libraries
and then inside the potty and the
guy will have references
to assume main.
In may not see we can
for as a main, we can basically tell
it that there exists a function
called Assa Main
and then for our variables we use
the extern keyword and see
to tell it.
This exists somewhere else, not
in this library.
And we're going to end up having
those external variables are going
to be stored in our shared library.
And so we're able to call as 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, well, hello,
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.
And those will also be modified
because RIP will change as it goes
through this program.
You can also see that both
that all Assa main prints
are going to be in the PLT.
And so when we do the call to those,
they're referencing the party and
not just the normal library
function.
So then when we try and run our
programs, logmein it out,
it'll basically say it can't find
LibertÃÂ© so and so
in order to fix that, you can
basically export the old library
path to tell the processor
or the shell where to look for that.
And so basically here we're
appending dogged the current
directory and then any other
library past that were defined.
So this is an environment variable.
And so we're just resolving the old
path and depending
on that.
So now when we run that
out, we end up with
getting those variables that we had
and we can see that we change them
a little bit to show the ASRM
Maine got called.
And then when we go through and read
the shared library,
we can look at the and I crop
this here so we could see it, the
dynamic symbols, we can see
that these get exported.
So hello to Assam, Maine,
and hello all get exported
as symbols so that the dynamic
Linko can look up those memory
addresses.
So today we talked about creating a
shared library.
And in the future, we're going to
talk about we'll do an actual
example of creating a shared
library,
if you have questions, you can email
me millirems you in Kada Edu,
and you can find me on Twitter at
Milhouse 30.