‫Hello and welcome, everybody, to part four of loading the Alpha.
‫Uh, let's just go back to the offloads and we're going to start creating some more functions in here
‫now.
‫So we're going to start with the offload function, which is responsible for loading.
‫And I'll file by by name.
‫So we just go in it.
‫Offload const char filename.
‫Struct, I'll file.
‫Fall out, so this is a multidimensional pointer, OK, we're going to return zero here.
‫So the idea is they pass the address of an of an alpha pointer, OK?
‫And then we will set that pointer to some memory we declare.
‫So here we're going to go struct.
‫Our file of file equals kasar block size of structural files.
‫So we're just making some room now in memory.
‫Would decline some memory to store that file in.
‫And fix that spelling mistake there, if you guys did the same.
‫So now we're going to go in at 50 zero and it rascal's f open file name, so we're only interested in
‫opening for reading.
‫We're not going to change this file.
‫If wrests is below equal zero, then there was a problem opening the files.
‫So we're just going to go out and we're going to have an out label by here as well, guys.
‫And we're just going to return wrasse.
‫OK.
‫Now, if everything went fine, then F.T. should equal terrorists, OK, which sets the fall points
‫here and now we're just going to say struct file stat.
‫Stats were basically about stap the file, OK, and then we're going to say rascal's f stat 50 percent
‫stats, the address stat.
‫So a stat in the fall because we want to get the file size.
‫If that failed then go to out.
‫OK, otherwise our file of memory equals K stat dot file size.
‫So we're basically creating enough memory on the heap to store the entire file because we're loading
‫the whole file into memory, the whole outfalls going to get loaded.
‫As I said before, a better implementation would be to use some sort of memory mapping and map function,
‫but we don't have that yet.
‫So this will have to do.
‫So now we're going to say rascal's Elfriede Alpha file of memory.
‫So we're reading the entire file into memory.
‫Now, start up file size one, OK, and then we'll just drift by that.
‫So now if the rest is below zero, then go to out.
‫There was an error.
‫Go to out.
‫OK, so brilliant.
‫At this point, the entire alpha is loaded into memory.
‫So when I was just going to say rascal's alpha process loaded and we haven't created this yet, this
‫function, but essentially it processes the loaded LTH file and if that fails, then we're just going
‫to go to our OK.
‫And then here we're going to go ask this file out Excel file.
‫So we're setting the point they provided to us.
‫OK, so that's what we're doing there because they basically go ampersand on the pointer to get the
‫address with the pointer variable.
‫They pass that to us and then we set what the pointer value is, essentially pointing the pointer variable
‫to the alpha.
‫We create it up here on line one, two, eight.
‫One last thing here, we're just going to go f close F.T. because once the whole file is being loaded
‫into memory, we don't really care about that file pointer anymore.
‫We don't plan to change the Alpha.
‫We just wanted to read it or read it all into memory, essentially, guys.
‫OK, so now we're going to create our process loaded function.
‫Just go in it process loaded struct and file our file.
‫OK, and here we're just going to go in and rescue zero return address, OK?
‫And now here we're going to say struct Elfreda had equals Elfrida our file.
‫So that gets the el-Fadl from the file.
‫Basically DKA pointed to it and as you know that's just a cast on to the actual Alaf memory, the alpha
‫memory variable in the file.
‫OK, it's just a case to see if we're going to say an address equals l validate loaded header.
‫So we're just validating that this file is supported by us and we're going to say if this is below zero,
‫go to out and we're going to make the out the out label here again, guys.
‫So this will first validate the file is is loaded correctly and and it's a valid L file.
‫And then after that, we just need to process the program headers because that's all we care about.
‫So we go equals F process P headers and we haven't created this function yet.
‫I'm going to pass in the file.
‫If the rest is below zero, then there was an error and we should go to our.
‫So as I just explained in the previous lecture, we're going to basically be loading the program ahead
‫into memory.
‫What we've already done that essentially we're going to be processing the program had is in memory.
‫That's what I meant to say.
‫That's all we care about is the program headers because we don't care about anything else at the moment.
‫We don't care about dynamic linking of runtime.
‫We just want to be able to run static our files with all statically compiled code, statically linked
‫code, essentially, guys, OK, we will not be able to load in the files that rely on shared libraries
‫or relocation files.
‫Essentially, all symbols must be known in the file we are loading before runtime.
‫OK, so now just just here we're going to say in itself, process P header.
‫So we're creating that function now struct our file, our file and we're going to say an address equals
‫zero return address.
‫And then here we're going to say, struct Elfreda header equals Elfreda, I'll file.
‫And I was going to say French, I cruzeiro I had to be numb, I plus plus so that a loop through all
‫of the program had is that we have OK, because remember the program head is stored in the.
‫OK, so now we're going to go struct of 32 for program header and we're going to call that variable
‫HDR and we're going to say equals elf program header and we're going to pass in the head and then we're
‫going to pass in AI.
‫OK, so that'll get us the program header with the given index.
‫And now here we're going to say Rascal's Elf Process P header and we're going to pass in the ELF.
‫And the program had a I remember if you're confused by any of this, check the specification because
‫it's huge.
‫It's thousands, probably a thousand pages long.
‫It'll explain everything in much more detail because this course is an operating system development
‫course, not an elf, cos I'm only teaching you enough about ELF to be able to load ELF programs.
‫If you want more of an in-depth explanation on that, you should check that out as it's true nesh to
‫be covered in a lot of death in this chaos.
‫OK, and we haven't created the process.
‫P had a function yet so just go f rest below zero break.
‫And if you do get confused guys, just let me know and I can explain all of this stuff in more detail.
‫Essentially what looping through the total number of program headers and then we're going to process
‫them by calling elf process P head.
‫So here we're going to go in it elf process P header and notice how we break everything down.
‫This is how code is supposed to be written.
‫We should be breaking everything down.
‫OK, struct our file, our file struct elf thirty two HDR program header and then we're going to say
‫an address equals zero switch r p type.
‫OK as well.
‫We care about the type of program had to type.
‫It is we only care about the load.
‫Remember I've explained this before, we're only going to be dealing with the load.
‫So here we're going to say wrasse equals elf process program had a load and we're going to pass in our
‫file and the program header.
‫So we're now soon to implement this function.
‫But before we do, let's just get the specification of ELF again so I can show you these things.
‫We can see peaty load.
‫By here.
‫So what is load the array element specifies a loadable segment described by peafowl size and PIMS size
‫the bytes from the file, a map to the beginning of memory of the segment.
‫If the segments memory size PIMS Z is larger in the file size, the extra box to define to hold the
‫value zero.
‫The file size may not be larger than the memory size.
‫So it explains all of this.
‫Right.
‫So essentially Pete Load describes a laudable segment and we want to process loadable segments.
‫OK, so we're almost done just at the very top here.
‫Now we're going to say in our process, we had a pretty loud struct elf file elf file struct ELF 30
‫to FDP.
‫So we're dealing with the program head for peaty load instances now and we're just going to return zero
‫here.
‫And this function will be in charge of calculating the virtual base address and the physical base address.
‫OK, so here we're just going to say if our file virtual base address is above our equal and we're going
‫to have to cast this to avoid pointer program had a P v address or the L file virtual base address is
‫equal to nothing.
‫In other words, we haven't set it yet.
‫Then our file virtual based address equals void pointer.
‫Program had a Peevey address, our file physical based address equals LTH memory, our file plus the
‫program had upset.
‫OK, so this here calculates the virtual based address and the physical base address.
‫Now we know the physical base address starts from the edge of memory, the one that we've loaded.
‫OK, and we know the virtual base address is specified in Peevey address because if we just go to the
‫dump health utility and we just go back in the programs into blank and we just dump that file dump health
‫plan, lf we scroll up in here.
‫You know, we can see the virtual base dressier zero six 400000.
‫OK, so we can see that zero x 400000.
‫So the virtual base dress should be zero x 400000.
‫OK, and so we essentially loop through all of these program load headers.
‫And if we find an address that is lower than the current address we have, then we know that the virtual
‫base address that we currently have is incorrect and we'll choose the lower value.
‫So that's what this statement is for.
‫OK, so for example, if it costs 400, 1000 and then it costs 400000, well, we've just set our virtual
‫base address to zero x thousand.
‫Well, we know that 400000 is smaller than that.
‫Right.
‫So we are above that in size.
‫So then we run this and correct ourselves.
‫We correct the mistake we made because we found a lower address and address that's lower than the one
‫we previously had.
‫So obviously, at the end of processing all of the program load headers, we will have a virtual base
‫address as the lowest possible virtual address for the program, which is exactly what we wanted because
‫we're calculating the base address.
‫OK, so we now want to calculate the end address as well.
‫So we're just going to go unsign in it.
‫And Virtual Address equals program had a P V address plus P program had a PDF file size.
‫OK, and now we're going to say if our file virtual end address is below our equal void pointer and
‫virtual address or the file virtual end address is equal to zero.
‫So we go our file virtual and address equals void pointer and the virtual address.
‫And then we also set the physical and address to the elf memory, the elf file plus the program header
‫P offset, plus the program had a PDF file size.
‫OK, so if you, if you use the dumbbell futility and go through this law makes sense.
‫OK, because for example, with the fiscal end address here, we're doing program based memory, the
‫elf memory, the physical based memory.
‫Plus the program had a P offset, which is four thousand eighty six plus the file size, which is thirty
‫one.
‫So that will obviously give us the physical address.
‫Right.
‫OK, and the virtual address here is calculated simply by taking the virtual the rest of the program
‫and adding on the size and then seeing if the end address is below the new the new one that we've calculated.
‫So we're basically doing the exact opposite is the one that we're doing to calculate the virtual based
‫address.
‫So that should be it for calculating the virtual base and end addresses.
‫So let's just make clean that now.
‫And if all works fine, then we're ready to continue.
‫So we're going to go build that LSH.
‫Uh, you can see it fails to copy across Blang Dobbyn.
‫That's because we in the last lecture, we changed the file to blank today.
‫OK, so we'll fix that in a moment.
‫But anyway, just scroll down through here because we need to make sure our float is available in the
‫offloaded files.
‫So just paste that here like that, OK?
‫And we just need to do one more thing.
‫Actually, we need to make an out of clothes, so we're going to go in it elf clothes, OK?
‫And that's going to take an our file.
‫And all we're going to do here is basically say if the file is not set, then we'll just return zero
‫and ignore it.
‫OK, because they passed us on no pointer in here, otherwise will free the file of memory and we will
‫free the file pointer itself for the elf file.
‫So that should be it for the elf clothes, because we declare memory here for the file and we declare
‫memory here for the elf memory.
‫So that should be absolutely fine.
‫And just copy and paste the elf clothes as well guys and know to return zero here as well guys.
‫OK.
‫In fact this can be void's, this can be void.
‫We don't care about that.
‫This can be void.
‫Just just use a void.
‫Guys, how am I going to copy and paste this into the header now?
‫OK, perfect, so let's just check to that builds one small.
‫Yes, it does.
‫Perfect.
‫So let's just fix that makefile problem quickly.
‫So just scroll down into the makefile here and you'll see that we're copying blank dobbyn just change.
‫That's a blank tarof, OK?
‫And then that'll mean that we're copying blanked out after Mounty then OK.
‫And we now just build again.
‫We can see that that works correctly now and we've copied blank to delve into the operating system code.
‫So we've kind of made our low load.
‫Now it's correct.
‫So what we're going to do on the next lecture, we're going to actually use the functionality we've
‫made here to load the file.
‫So that's in task process.
‫Start see, you'll see that we have the process slow binary or we're going to make a new one called
‫Process Load LTH, OK, and we will basically load the whole file essentially.
‫So thanks so much for watching and take care.