Hello, this is Dr. Miller, and this
is Episode fifteen point seven of
Assembly.
Today, we're going to talk about
writing a shared library in
assembly.
So our shared library example.
So what's our problem to be solved?
So we're going to write some code
and a method inside of assembly
and then we're going to use C calls
to reference that.
And what we're going to do is we're
going to set up some writable and
executable memory.
It's also readable and then
we're going to copy some assembly
code into that memory and
then we're going to get a function
pointer and then execute
the code inside of that buffer.
So it'll involve a lot of the
different things that we've talked
about throughout our class.
So this is based
off of the previous lecture where we
talked about creating a shared
library.
So we can see here that we have all
of the code to build
our shared library.
So we'll start with creating
a little bit of code that we're
going to execute.
So.
In here.
We're going to create a text segment
and we're just going to do a move
for us, so move IEX.
Index.
All right.
The gospel segment, right?
And so what we can do is we can
get the bites out of this.
So I'm just going to
show you the get bites code.
So basically, we're going to we can
compile that assembly into an object
file and then we can use object to
basically pull out the bytes of
that.
So here it created our
object file.
And then we're going to see
these are the bytes that will do
move X and then one, two,
three and X.
So we're going to just copy
these bytes out
and we're going to end up putting
those inside of our
assembly file.
So we want to declare a piece
of data that we're going to be able
to use, so we're going to make this
global.
We're going to tell it,
tell the Linko that this is data
and then we're going to allocate
16 bytes for this.
So here's our buffer.
And then so we're going to put
all these bites here.
So one of the things that we should
know is that we've got to put we
want to make these Hecks bites
because that's what was outputted.
And then because this one started
with the B. M, I think it's a
variable. So I got to put a
zero in front of it.
All right, and then I'm going to
allocate some empty space here, just
to be sure you can change your sizes
here, but
we'll allocate
16.
No bites inside of
their.
So that we can
reference that and then
inside of here,
we're going to.
So we're basically going to fix our
code. So I didn't put a return
statement in.
And so I know the
code for a return statement is C
three in hex.
And so we're going to move.
So this is offset zero.
One, two, three,
four.
So we need offset five.
We'll put that here, move
by.
So here we need to load the address
here, we're setting
byte five
to our knowledge, so we got zero
one, two, three, four.
So this is the fifth byte
we'll set.
And here we need to load the address
of Buffer.
Now, again, this is going to be
relative to the similar table.
So with respect, with reference
to the
simple table.
And then we'll go ahead and finish
our code here.
And so we're going to want to call
our assumption so that this
function will actually return
when we're all done.
So we'll just make sure that we call
that and we can see that we set
global as a main
function. So that way it is a
function that we can
call if we need to.
So that's our assembly code and
then inside of our main code.
I've already included some of these
things, so I don't have to
necessarily type them all out.
I got standard IO twice, so we have
standard IIO.
We can use some string library.
I don't know if we'll use that one
actually in this one.
One of the things that we're going
to use is the memory manager.
So we're using a
map, so we're doing a memory
map.
And so what the memory map is
going to do is it'll allocate
us a certain size of memory
and then it could give us different
protections.
So that's the property.
And so we want that memory to be
readable, writable
and executable.
So we're going to be running some
code and we're going to copy some
data to it,
and then we're going to map that to
private and anonymous.
And so it'll it'll allocated
for us right away and
use the anonymous flag, which I
had to define up here, because
some versions of Linux don't work
properly for that.
Now we want to be able to use as a
main. And again, that's going to be
dynamic so we can say into ason
main and then we can call that
in order to patch up our bytes.
And so what I want to do is I want
to copy the data from Buffer
into this data
location.
So at the top, I need to
tell it that buffer exists.
So that's going to be extern
you
and a underscore
t we
learned about that
and then I'm going to allocate 16
bytes for that.
And this is basically just telling
it that it's 16 bytes wide,
but it's external.
And so it doesn't it's just it can
actually have a pointer to it.
So the size probably really
doesn't matter.
And so then what we want to do is we
want to copy the data from the
buffer into our
data variable.
So we'll just write a for loop.
And then we'll copy data from
one into the other
room
so we can say data
at eye
is equal to
buffer at eye
and making sure you get these sizes
right. So I want these to be bitts
because I want to copy all of the
bytes from one to the other.
And so both of these are I'm just
going to use ONT 80
to make sure that these get to be
bytes and that we don't have any
sign extension breaking
things inside of those.
All right. So now we need to
create a function pointer.
So again, this is something that
we've learned before.
And so.
What we want to do is we
want to get the result of our
function and so it's
going to move the value into X
or Arax and so we can
go ahead and declare our function.
So you and.
And so we'll just copy a thirty
two bites for it,
so 30 to underscore T,
so that is the return type of our
function.
And so we're going to
have a function pointer, it's
going to take no arguments, so it's
going to be a void function
and we'll say that is
so then we're going to we've
got to put our cast here.
So the return type again, you and
thirty to underscore t
this is a pointer
and then the function takes
void or no arguments.
And so we're going to turn data into
a function pointer.
And so then we can
basically use F as though it's
a function that takes no arguments
and it returns a U.N.
thirty to underscore t right.
And so we moved data
into X and
so we can get the result of that.
So you end thirty two,
underscore t
value and then we're just
going to call
the function F
and then we can print off that value
so we can do print F.
I'll put a zero X in the front
and then percent zero
eight X
put a new line
and then I'll put my value.
So we will end up referencing
Buffer and then referencing the fact
that as a main is
going to get executed.
You know, looks like I have a syntax
error here on line five.
It's four or five.
Oh, I misspelled this.
All right.
So I also have this update path
that will let me export
that shared library so that I can
see it.
And then I should be able to run.
And we get to value one, two, three,
four, so and then I'd like to debug
its just so we can see.
Now, you can see that I added
debugging symbols and so then we can
see the C commands
inside of here and we can also see
the assembly commands.
So
let's just do
next to
go over the different functions.
So what I'd like to do
is I'd like to skip my loop.
I just want to come down here so I
can just say break.
19, I can continue
over my loop, so
here we're going to see that we're
going to
skip over the data so we can print
off what that data.
Zero,
one, two,
three, four.
And five, so we
can see that the
RSM main got called and it modified
data so that it had the right values
because we know at the end we added
our C3 inside
of their.
And so then we can do
our cast.
And then what I want to do is I'm
going to step into our function
so we can see that
we have our buffer and we're going
to just step into that.
So I'm
going to go ahead and use step
instruction and then we'll go over
each instruction so it'll copy
the pointer into RSX
and then it's going to go ahead and
call RSX and then we
can see our code move yak's.
One, two, three, four.
And then when I patch the bite's,
I added a return in there.
So that again is the next symbol.
And so then our value gets
set and then it gets copied
into our value and then print
it off.
So today, we talked about building
an assembly dynamic library, using
a lot of the things we've talked
about in class in order to get that
done,
if you have questions, you can email
me, Miller, M.J., you indicated Edu,
and you can find me on Twitter at
Millhauser 30.