‫Hello and welcome to Part six of implementing the Health Loda in this lecture, we're going to change
‫the way that we map the file.
‫Currently, we just map from the start virtual address to the virtual address and we just make the entire
‫thing readable.
‫In real life, we care about memory being read only.
‫So if we just go to the blank program and we go read Dash s blank dot, lf you will see the symbols,
‫use the capitals instead and that'll show us the sections and we can see here flags.
‫OK, so we can see that the data section is readable, ok.
‫It has a W flag.
‫It's readable.
‫OK, now this is important.
‫We do want to care about these flags and by caring about these flags we can ensure that the memory that
‫should be read only is read.
‫Only if you just do a lowercase A as the option, it will show you all of the information about the
‫file.
‫We can see the program headers.
‫These are the ones we're going to be worrying about.
‫So you can see this, this, this these virtual addresses are readable and these virtual addresses are
‫read and readable.
‫So you can see from virtual address 2x 400000 up until the the length that is readable only and then
‫from zero x 400, 1000 up until the length that is read and rideable.
‫OK, so these program headings, we are going to process them correctly and ensure that we attach the
‫correct flags rather than just mapping the entire memory ranges rideable.
‫OK, so we're going to do that in this lecture and we're also going to set the entry point, which we
‫haven't yet done if we just go to.
‫TASC New.
‫We see Taskin at that disco there, we can see we said the IP to the program virtual address.
‫Now that's all fine that's expected.
‫However, if the if the program were loading is a file, then we we absolutely should be setting it
‫to the entry point stored in the header file which is, which is here.
‫Entry point address you see.
‫So this is the one we need to take care about.
‫So we're going to be doing those two things in this lecture today.
‫So let's begin.
‫OK, so are first going to start by mapping the files.
‫So just got a process map elf function we made and we're going to change the code in here.
‫So just by here we're going to go struct.
‫Elfreda had a equals Elfreda our file and you'll notice that it looks like it's not defined.
‫So if we just good outflow to see and we're going to move a lot of this stuff to the head of file so
‫that it's available outside of our flow to not see.
‫So just take the header.
‫We're going to put that in the outflow.
‫Florida.
‫Take the al-Fassi header, we're going to put that in the loda.
‫OK, take the elf memory, we're going to put that in the low in the header file, not the load of the
‫header file, and we're going to put the P head in there as well.
‫And finally, the program had a function here as well.
‫And you know what, let's just put the section there, too, because we never know, maybe we want that
‫at some point.
‫So just put that that to OK, so that's it for that.
‫So just put all those in the head of file so that they're accessible outside of flow to see go back
‫to process, start see.
‫And now we can continue here.
‫You'll see that our floater is now resolved correctly.
‫So we're just going to go of 30 to program had a program headers.
‫Make sure you have that.
‫S on the end f p hadda hadda.
‫So now we're just going to go for integer IQ zero ie below head EP and AI plus plus.
‫Now obviously this is the number of program headers because as I showed you here, we are interested
‫in going through all of the program headers.
‫Which by here and basically mapping them into memory, OK, their virtual address should be mapped to
‫their real physical address in memory, not the physical address they provided here.
‫OK, so in here, we're just going to go structure of 32 program head up idea equals ampersand program
‫head is I now hear we're going to go void's program had a data address and this is the physical address
‫of that program ahead.
‫So we can just call it a physical address, actually.
‫OK, equals ELF program had a physical address.
‫We haven't created this function yet, but we will.
‫And this function will be in charge of calculating the physical address for the program.
‫How do we provide to it?
‫Now here we're going to say in it Flagg's equals paging page.
‫While is presence, I should say paging is present, paging access.
‫From all I hear, we're going to say if the program had a P, Flagg's has the right flag.
‫OK, then this, this section, while this these pages in memory should be writable.
‫So we just go flags and we all that on we are the paging is rideable flagged onto that Flagg's variable.
‫OK, so if the program had a should be rideable will mark the flag as rideable and this will be the
‫flags that will pass here to the actual paging system.
‫So we're going to copy and paste what we've done here and we're going to paste that here, OK?
‫And what we're going to say is.
‫Uh, we're no longer caring about the outfits from the dress we now care about the program had
‫PVA dress OK, which will give us the address specified here, the virtual address.
‫OK, so that's what we care about here.
‫Uh, the physical address while the physical base address here will be.
‫The program had a program, had a physical address.
‫OK, however, we must also align this the lower page, so just go paging line to lower page like that.
‫OK.
‫And now we are going to want to align the end address as well, but we're going to get rid of this because
‫this is the physical address.
‫OK, so that's going to be the program had.
‫A physical address, plus the program had a peafowl size, OK?
‫And get rid of these flags we put here and we're going to use the flags variable like that.
‫OK, let's just go through this, make sure it's right.
‫So, yeah, we want to we want a map from the fiscal page address.
‫To the end address and the end address, is the next page up essentially or basically not, let's say
‫the next page up, it's the physical address plus the file size, and then we align that to the next
‫page up from this value of this computation.
‫OK, so that looks absolutely fine.
‫Uh, just going to scroll through this slowly for you so you can see and just pause the video if you
‫need to understand it more, because I can't fit it all on the screen.
‫So, yeah, that's how it should look.
‫OK, guys, now.
‫If there's an error with the rest, then we just break we were not interested in processing anymore.
‫If there's an error, so this should map the profile correctly.
‫OK, so that's that bit done.
‫Let's implement the program.
‫Had a physical address function, so just copy this and we're going to paste it now.
‫Floated out.
‫See now.
‫OK.
‫So just by here, we're going to copy that and paste that in, that's going to return a voice pointer.
‫OK, guys, it's going to accept an error file and a program header.
‫And I spoke that wrong the other.
‫So now the equation for this, the math for this is quite simple.
‫We just go return our memory file.
‫OK.
‫Plus, the program had a be offset, I think is Karpoff said.
‫Let's double check that.
‫Yeah, percent call.
‫So that'll work, and I think I think you can see what's happening here.
‫Essentially, we know that the fiscal address is going to start from the elf memory where we loaded
‫it in memory.
‫And we know that it's going to be an offset from the beginning of the el-Fadl.
‫Well, because we loaded the entire ELF file to the elf memory here.
‫We know that the the absolute position will be the address of the elf memory, plus the offset in the
‫program.
‫So that'll give us the physical program had to address in memory.
‫OK, so don't forget to put that in the header file as well, because we want we want to be able to
‫access that outside space that in the back post to start see, we can see that now.
‫It looks completely fine.
‫OK, so we're now going to go to Task Stazi and we're going to set that entry that I was talking about.
‫So go to the task in it and the IP here.
‫We're going to send it to the program entry if it's an elf file.
‫So what we say is we say if process file type equals file type elf.
‫OK, then put this under here as well guys.
‫Then task registers
‫the IP equals process and file.
‫And you'll see it's not letting us resolve this, so we need to include the header file, scroll to
‫the top.
‫We're going to include
‫Loda Format's outflow, the DOD.
‫OK, and then go back here.
‫We're going to say our file and we're going to call Elfreda here.
‫And then we're going to say easy entry, so should look like that, OK?
‫So if the process type is and how far will set the IP so that the entry, which in our case blank that
‫I assume is correct, 400000, it might be something else for a different executable that's being loaded.
‫And you'll notice I don't use Elsia.
‫Why don't I use else.
‫Because it's cleaner, OK, it's cleaner to set the IP to what it should be usually for binary file
‫and then to change it later.
‫It's far cleaner than having an else statement.
‫OK, maybe we use one or two more cycles.
‫Who cares?
‫This is much cleaner than having some else statement, so I tend to try to avoid false statements where
‫possible because they can make the code very ugly.
‫The NSF is OK.
‫I rarely use URLs.
‫There's many ways to do the same thing without using an mouse, for example, here, much cleaner to
‫set the IP and then change it later on.
‫OK, so that all looks fine.
‫When are going to test this and see if everything works as expected.
‫OK, suggests in the terminal we're going to go make clean build that S.H..
‫And what I am going to run Kumu Kusumi System I 386 Hajja or Stuben.
‫And now use loaded and we're just going to start typing and we can see everything works, works as expected,
‫so there's still no problem.
‫And now we're loading the sections correctly because we're taking the flags into account, which ensure
‫that certain areas in memory are only readable.
‫So it stops you writing to sections like code sections and stuff.
‫Write code shouldn't be modifiable.
‫And whilst it's in memory, so those those should definitely be set as read-only.
‫And because of this code we wrote here where we go through all the program headers and get the correct
‫flags, we've resolved that problem.
‫So congratulations.
‫You've now successfully create an outflow that can load static ALF programs if those programs depend
‫on shared objects or well, basically libraries, unfortunately, you won't be able to load those yet.
‫However, you do have something that can load statically linked outfalls.
‫So congratulations.