Hello. This is Dr Miller. And this episode 15.7 of Assembly.
Today, we're gonna 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, um, and a method inside of assembly. And then we're going to use C calls to reference that. And what we're gonna do is we're gonna set up some rideable and execute herbal memory. It's also readable. Um, and then we're gonna copy some assembly code into that memory,
and then we're going to go get a function pointer and then execute
the code inside of that buffer
so it will 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 I'm 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
we're going to create a text segment, and we're just going to do a move for us. So movie a X
Ah, spill 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 weaken, compile that assembly into an object file, and then we can use object dump to basically pull out the bites of that.
So here it created our object file, and then we're going to see
these are the bites that it will do. Move yea X and then 123 in hex.
So we're going to just copy of these bites out,
and we're gonna 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 gonna make this global.
We're going to tell it.
Tell the linker that this is data, and then we're going to allocate 16 bytes for this.
So here's our buffer,
so we're gonna put all these bites here. So one of the things we should know is that we gotta put we want to make thes hex bites, cause that's what was output id.
And then because this one started with the B, it might think it's a variable. So I gotta put a a
zero in front of it,
All right? And then I'm gonna allocate some empty space here just to be sure you can change your sizes here, but,
No bites inside of there.
and then inside of here,
so we're basically going to fix our code, So I didn't put a return statement end. 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. 01234 So we need offset five.
We'll put that here. Move,
So here, we need to load the address. Here. We're setting, um,
Um, to our nose, we had 01234 So this is 1/5 bite.
We'll set. And here we need to load the address of buffer. Now, again, this is going to be relative to the symbol table. So with respect, reference to
and then we'll go ahead and finish our code here.
And so we're gonna want to call Ras the main so that this function will actually return when we're all done. So well, just make sure that we call that
and we can see that we set Global Asan 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 been already included some of these things eso that I don't have to necessarily type them all out.
I got standard io twice. So have standard io we can use, um, a string library.
I don't know if we'll use that one. Actually, in this one,
um, one of the things that we're going to use is the memory manager. So, um, we're using em map. So we're doing a memory map.
And so what the memory map is going to do is it will allocate us a certain size of memory, and then it could give us different protections. So that's the p. R. O. T.
And so we want that memory to be readable, rideable and execute herbal. So we're gonna be running some code, and we're gonna copy some data to it.
Um, 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 Lennix don't work properly for that.
Now we want to be able to use a S on main, and again, that's going to be dynamics weaken, say, into Asan Main. And then we can call that in order to patch up our bites. 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, um, tell it that buffer exists.
So that's going to be ex turn
and a underscored T.
We learned about that,
and then I'm gonna allocate 16
um, bites for that. And this is basically just telling it that its 16 bytes wide but its external and so it doesn't It's just you 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,
variable. So we'll just write a for loop,
and then we'll copy data from
So you can say data at I
buffer at I and making sure you get the size is right, Right. So I want these to be bites cause I want a copy, all of the bites from one to the other.
And so both of these air, I'm just going to use you went 80 to make sure that these get to be bites 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 have learned before.
what we want to do is we, um, want to get the result of our function.
Um, and so it's gonna move the value into the A X or R E X, and so we can go ahead and declare our function. So you and
and so we'll just copy 32 bytes for it.
So 30 to underscore t.
So that is the return type of our function.
we're going to have a function pointer. It's going to take no arguments, so it's gonna be avoid function
and we'll say that is
so Then we're going to got to put our cast here. So the return type again, you int
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 30 to underscore t
right. And so we moved, um, data into the X, and so we can get the result of that.
So you end 30 to underscore t
value. And then we're just gonna call
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 08 x
and then I'll put my value
so we will end up referencing buffer and then referencing the fact that Assam main is going to get executed.
Looks like I have a syntax air here online five
for five. I misspelled this.
So I also have this update path that will let me,
um, export that shared libraries, that I can see it,
and then I should be able to run.
And we get the value. 1234 So and then I'd like to debug. It's just so we can see
now you can see that I added to bugging symbols, and so then we can see the sea commands inside of here, and we can also see the assembly commands.
to go over the different functions.
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, um,
skip over the data so we can print off. What's that? Data
So we can see that the A s M main got called and it modified data so that it had the right values, because we know what the end we added our c three,
um, inside of there.
And so then we can, ah 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
Uncle, who go ahead and use step instruction and then we'll go over each instruction so it will copy the pointer into our X, and then it's going to go ahead and
call Ari X. And then we can see our code movie a X 1234 And then when I patch the bites, 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 U N K i e. To you and you can find me on Twitter at Milhouse 30