WEBVTT 1 00:00:02.040 --> 00:00:10.200 William Cheng: Okay, this is part three of lecture eight. So we're going to talk about next part in chapter three and this is about linking and loading. 2 00:00:12.480 --> 00:00:23.010 William Cheng: Alright, so I guess we're going to go back and look a little bit of a assembly language. Again, I know it's not very fun but then any organ. Look at that very simple case over here. 3 00:00:23.670 --> 00:00:35.190 William Cheng: So this is the assembly co for the main function on that only return or exceed as it turns out. One interesting. Um, I'm gonna, I'm not gonna walk through this code, you know, 4 00:00:35.700 --> 00:00:42.390 William Cheng: So just sort of executing right here. The question that asks over here is that does location matter. 5 00:00:42.810 --> 00:00:56.190 William Cheng: Okay. As it turns out, this piece of code can execute anywhere in your address space and the stock frame OVER HERE. THE STAFF ROOM can actually be located anywhere in your, your address space this code will work exactly the same. 6 00:00:57.210 --> 00:01:09.270 William Cheng: Okay, so in a way this code is location independent, it doesn't really matter where you put this code is. And as long as you're ESPN MVP points to anywhere is how you address space. This call is going to work. Okay, so. So over here, it says, you know, 7 00:01:10.290 --> 00:01:12.870 William Cheng: As you know, the, the other location that depend on up. 8 00:01:13.530 --> 00:01:20.580 William Cheng: So if everything can be accessed relative to the frame pointer. Well, then the frame can be anywhere is how you address space, right. Again, this is your stack frame. 9 00:01:20.730 --> 00:01:28.290 William Cheng: You can put it, put it anywhere as long as you point at the right place ESP points at the top of the stack rain EDP points in the middle we hear link all the staff rooms together. 10 00:01:29.070 --> 00:01:39.300 William Cheng: then then then then this doesn't matter, right. So this code is location independent. Okay. So that begs the question, what kind of code is location dependent. Okay. 11 00:01:40.260 --> 00:01:48.930 William Cheng: So. So as it turns out, if you use any kind of code. They use a global variable or if you make a function call, while then in that case the function can actually get move around in the address space. 12 00:01:49.230 --> 00:01:53.580 William Cheng: Okay, the global variable. If you add another global variable. Some of the goal BeBo, it will get moved around. 13 00:01:54.120 --> 00:02:01.230 William Cheng: Because the goal of elbow again that live inside of tech segment and that data segment the size of those segments depends on how many global variables that you have 14 00:02:01.530 --> 00:02:06.510 William Cheng: Right, so the more you have, well then, then you know the the address. The address. We'll get we'll, we'll get moved around. 15 00:02:07.020 --> 00:02:14.010 William Cheng: Same thing with your tech segment right the attack seven is all your program. So, so all your code and your code is divided into functions. 16 00:02:14.190 --> 00:02:21.900 William Cheng: The more functions you have you know the the text admin is going to be get get get bigger. And also, every time we add another function, the function is going to get a different address 17 00:02:22.830 --> 00:02:27.780 William Cheng: Okay, so in that case the CO you generate they will become location dependent. Okay, if you 18 00:02:28.410 --> 00:02:34.140 William Cheng: If you move all these functions around. Well, if you still keep the old address and then in this case not gonna work. 19 00:02:34.950 --> 00:02:40.830 William Cheng: That. So what we need to figure out is, I mean, this example we have we have global variable x over here. What is the address of x. 20 00:02:41.400 --> 00:02:48.420 William Cheng: Okay. When do we know what is the address of x. Who knows what the edges of x, you know, so, so I think, as soon as I saw this kind of question. 21 00:02:48.630 --> 00:02:53.700 William Cheng: What is the address of x come from. And so that's what we're going to, we're going to go look at now where we're going to look at Lincoln and loading. 22 00:02:54.000 --> 00:03:01.500 William Cheng: As it turns out, is the linker that tried to figure out the addresses of all these global variables. OK. So the goal variables over here and also these 23 00:03:01.800 --> 00:03:08.400 William Cheng: The these function pointer is the function, but it also kind of like global variables, right, except that they don't live inside the data center or PSS 24 00:03:08.940 --> 00:03:14.790 William Cheng: They live inside of tech segment. Okay, so all these global variable over here. You know what, what are their addresses. 25 00:03:15.180 --> 00:03:26.310 William Cheng: You know, when do you know what the actual addresses. I mentioned before, once you start running your program. The address doesn't change anymore. Okay, so at what point do you actually know the address of all these global variables net 26 00:03:27.990 --> 00:03:42.750 William Cheng: So who, you know, who, who, who, who actually, you know, put the actual value of a, of so so this this call over here says hey of x is equal to the edges of x. Okay, so who initialized a of x over here. Okay. So again, we're gonna we're gonna see you know how this is done. Yeah. 27 00:03:44.580 --> 00:03:53.520 William Cheng: So the way this is done so got a long time ago, the compiler people the offices and people to hardware people they got together and sort of figure out who's going to do what get who's going to determine the address. 28 00:03:53.940 --> 00:04:02.430 William Cheng: Is it tonight. This is done in two steps. Okay. The first step is called compilation. Okay, well you compile the code we compile the code like this, what are you doing when you compile the code. 29 00:04:03.060 --> 00:04:09.660 William Cheng: Well guys, the first of all you're gonna check for errors, right when you figure out checking for the arrow over here, you try to sort of figure out how big is the address face. 30 00:04:10.200 --> 00:04:18.930 William Cheng: Okay. So, for example, the data section over here. How big of a data segment you need. Okay, so, and also how big of a tech segment you need. So once you have the tech segment that data segment the BSS segment. 31 00:04:19.110 --> 00:04:24.990 William Cheng: Once you figure, all these things out, you can determine the addresses for all these global variable for all these function pointers. 32 00:04:25.680 --> 00:04:35.310 William Cheng: Yeah. Alright, so, so, so the first step over here is that, is that what we're going to compile the code. And when we compile the code. We start with a dot c file and we generated that old file. 33 00:04:35.730 --> 00:04:44.520 William Cheng: There. So in this case, we try to sort of figure out how many bytes every object. Yes. Right. Will you declared an array, the array. How big is it about the compiler will figure that out. Okay. 34 00:04:44.730 --> 00:04:51.000 William Cheng: And what we're going to do that we're going to assign them a temporary location because at the time when we come out. We don't really know what their final location as 35 00:04:51.210 --> 00:04:59.010 William Cheng: Right, especially if you do a separate compilation. Now if you do separate compilation. You know, like a one on one and two, one more, one dicey get compiled into 111 dot. Oh. 36 00:04:59.190 --> 00:05:05.580 William Cheng: Okay, all the addresses in one.one.oh they're all temporary addresses and then we come home I forward to this scene to my fourth 37 00:05:06.120 --> 00:05:14.340 William Cheng: Oh, and if you look at the address is the over there, you know that you know some, some of the function, nobody I have exactly the same address as the one, one last one. 38 00:05:15.870 --> 00:05:23.520 William Cheng: Okay, so, I mean, clearly, you know, these function over here. They don't really have the same address. So finally, when you link them all together. Right. So the second step over. It was called link when 39 00:05:23.760 --> 00:05:30.870 William Cheng: When you try to link them all together. You take all the data files together and you put them all together one. In that case, you shouldn't have to function, you have the same address 40 00:05:31.470 --> 00:05:40.050 William Cheng: Okay, so therefore it's the linker that plan out your address space to determine every function. What kind of address, they get and also for every global variable, there was sort of figure out what what will 41 00:05:40.530 --> 00:05:47.910 William Cheng: I will forever global level, they will figure out what address, they will get there. Alright, so the linker is one that figured out where every object is 42 00:05:48.810 --> 00:05:59.610 William Cheng: It is it is located so so so the linker is the one that plan out your address space. Okay, the compiler determine every object, how big they are. So this way. The link will add up all your 43 00:06:00.810 --> 00:06:06.630 William Cheng: Support for for the for the idea, you can add them up, you can actually figure out what is the size of your, of your memory segment. 44 00:06:07.560 --> 00:06:09.210 William Cheng: Okay, so this is starting to set now. 45 00:06:10.020 --> 00:06:17.430 William Cheng: So, so the idea where we location is that, you know, when you compile your code, you get a temporary location right and then the linker find out your final location. 46 00:06:17.580 --> 00:06:24.780 William Cheng: So you know what will happen is that your functional your global variable at the beginning. They have a temporary address and then they get relocated to the final address 47 00:06:24.990 --> 00:06:33.750 William Cheng: Okay, just like what you come to the United States at the beginning, you're going to get a temporary address. And then when you find out your final address, you're going to tell the post office to for all your mail, you know, to your final address 48 00:06:34.020 --> 00:06:44.790 William Cheng: OK, so the job of the linker is to perform relocation that. All right. So, by the way, it's over here is that is the program that performed relocation is known as the linker 49 00:06:45.450 --> 00:06:50.760 William Cheng: So again, chapter three, we're going to start seeing that the textbook start making mistake for somebody to text, text will actually said that 50 00:06:51.210 --> 00:06:56.910 William Cheng: The program that performed relocation is called a loader. So that doesn't really make sense. Right. You know, so, so, so 51 00:06:57.810 --> 00:07:11.220 William Cheng: The blinkers is the one that do that for Linux and Unix system. The name of the linker is LD ok for somebody. It's called it's cloudy but LD look like a loader. So maybe that's one of the reason that you know the test, but I should make that state now. 52 00:07:12.450 --> 00:07:17.040 William Cheng: Alright, so when you perform relocation, you try to plan out the address space. And what you will do is that you 53 00:07:17.910 --> 00:07:21.930 William Cheng: The linker will take all that information and he will generate the executable file. 54 00:07:22.920 --> 00:07:30.390 William Cheng: Okay, so the result of relocation is that every, you know, every object, get a final memory location and all that information is stored inside the executable file. 55 00:07:30.600 --> 00:07:38.640 William Cheng: And later on when you ready to run your program you double click on your program will happen, right. So, in that case over here, we double click you know your your your your window shell. 56 00:07:39.150 --> 00:07:45.510 William Cheng: Will for for coffee child process in the Chopra said they called exact inside the exact what he would do is that they will invoke the loader. 57 00:07:46.200 --> 00:07:50.160 William Cheng: Okay, we mentioned before, what, what, what is the loader do right the loader. Take a program from the desk. 58 00:07:50.460 --> 00:07:54.750 William Cheng: And folded into memory and then transfer control to it. Okay. So in this case, what 59 00:07:54.990 --> 00:08:02.940 William Cheng: You know, when I say transfer control to, oh, what is the go to right so what it will do it will say he IP to the start off on shooting and the sort of function will call the main function. 60 00:08:03.270 --> 00:08:10.980 William Cheng: Okay, so that is the job with the loader right the loader copy this call unfolded into memory and then he was set up to point to the startup team. 61 00:08:12.180 --> 00:08:16.950 William Cheng: OK, so again that's linking and then followed by loading right okay so so that that's what talking about 62 00:08:17.820 --> 00:08:26.100 William Cheng: Alright. So as it turns out, there are two functions for the linker. One is, before we location right figured out for the final addresses for every global variable and every function. 63 00:08:26.550 --> 00:08:29.550 William Cheng: There's another thing that it will do is that it costs symbol resolution. Okay. 64 00:08:30.060 --> 00:08:36.540 William Cheng: So for example, if your program your corporate F right printer this you know it's not a function that you, that is not a function that you wrote 65 00:08:37.080 --> 00:08:47.250 William Cheng: So, so who actually find the code for printer, right, because in the year where you're trying to create an executable program the executable for is ready to run because the loader simply unfolded into memory and this are running 66 00:08:48.150 --> 00:08:56.610 William Cheng: Okay, so therefore your program sitting on this that program must have the printer function, right, because if it doesn't have the print function. How do you add your own folder and start running 67 00:08:57.270 --> 00:09:08.820 William Cheng: Okay, so therefore it's the job of the linker to make sure that a copy the code for printed copy into your executable. So this way you know when we, when you turn on your program, you know, you have the code already so therefore you're there for you get to run 68 00:09:10.380 --> 00:09:16.080 William Cheng: Okay, so, so, so again the link has two functions. One is relocation relocation. The other one is the symbol resolution. 69 00:09:16.290 --> 00:09:27.570 William Cheng: When it finds that printer function is not something that you have implemented what they would do that, it will go to the C library and then find that function and then copy all the necessary ko from the front of C library into executable. 70 00:09:28.200 --> 00:09:35.100 William Cheng: Okay, what about malakal free were in the last, you know, in part two of the lecture, we talked about Malik and free. They also have the the free list. 71 00:09:35.310 --> 00:09:45.360 William Cheng: All those kind of data structure. Again, instead of C library. They have all those all the information for that function, they will get copy into your executable. So we provide is ready to run it will have all that information. 72 00:09:46.410 --> 00:09:46.680 William Cheng: Okay. 73 00:09:47.790 --> 00:09:54.300 William Cheng: So, so this is done at compile time. And this is not a link time. Okay, so, so, so this is one way to do it. 74 00:09:54.840 --> 00:09:59.400 William Cheng: Towards the end of the semester. We're going to see another way to do it. And that is called dynamic Lincoln and loading. 75 00:09:59.520 --> 00:10:08.940 William Cheng: So at the time when you create your executable you actually, you don't know you. Actually, you actually don't need the information for for what we're, we're all these functions are well your program is running. 76 00:10:09.150 --> 00:10:14.190 William Cheng: At that time, the awkwardness is more dynamically load function like print up into memory and then you can call it 77 00:10:14.760 --> 00:10:21.570 William Cheng: Okay, but that's a little more advanced, we're going to leave it towards the end of the semester. Right now, we're only we're only worry about static Lincoln and loading. 78 00:10:21.810 --> 00:10:30.090 William Cheng: Right, which means that at compile time. The link time you figure out exactly why the outer space. Look, while you figure out the memory location where everything and then you copy all those code. 79 00:10:30.420 --> 00:10:38.190 William Cheng: Into executable file and now your executable file is ready to run. Okay, so a little different. So I'm going to talk about what that I mean LinkedIn and load is all about. So, for 80 00:10:39.000 --> 00:10:45.210 William Cheng: For for right now. What you need to know is that the executable file has all the information that you need to run your program. Yeah. 81 00:10:46.920 --> 00:10:53.940 William Cheng: Alright, so we're gonna take a look at the example here. So this example we are, we're gonna have a main function that calls a sub routine over here. The main function is 82 00:10:54.600 --> 00:10:58.680 William Cheng: The main function is going to sit inside main Darcy, the sub routine over here, it's 83 00:10:59.160 --> 00:11:04.050 William Cheng: Inside of function instead of pockets of our Darcy. Right. So in this case, what are two separate compilation 84 00:11:04.260 --> 00:11:14.430 William Cheng: We're going to compound main does seem to me that, oh, we're gonna come to us apart. I seem to Kosovo and then going to link them all together and generate the executable the executable name is going to call it is going to be known as proc 85 00:11:15.180 --> 00:11:20.970 William Cheng: Okay. As it turns out, in order for you to generate Prague. All you have to do something like this, you can say GCC minus oh Prague. 86 00:11:21.150 --> 00:11:28.650 William Cheng: And then you just named all the dot c file and what they will do is that when you run your program like this they will actually do separate calculation internally. 87 00:11:28.830 --> 00:11:36.630 William Cheng: To compel Mendes intimate our own stubbornness into words about oh, and then it will invoke the linker to link all of us together to generate the executable program. 88 00:11:37.380 --> 00:11:41.850 William Cheng: Okay. In one and two, I asked you to do this explicitly right to separate 89 00:11:42.240 --> 00:11:50.850 William Cheng: A compound adult sci fi into the log file using one UNIX command and then here's another UNIX command which is the GCC command to compile, you know, my phone to listen to my photo. 90 00:11:51.090 --> 00:11:59.640 William Cheng: And then finally, you going to run GCC one more time and this time Jesus. He only invoked the linker to link all the data files together to generate a warm up one of the one or two executable. 91 00:12:00.690 --> 00:12:08.520 William Cheng: Okay, this example. So to show you can you can do it on one line. But as you've seen in the grading guidelines if you do that in one month one and two, you're going to end up losing 92 00:12:10.770 --> 00:12:13.320 William Cheng: I think you're gonna lose 10 points or something like that. Right. 93 00:12:14.130 --> 00:12:23.250 William Cheng: Alright so this guy's we're going to separate compile them right i mean that's he's going to combine two main.oh so main.io so bar. Oh, they are know as we locatable module. 94 00:12:23.580 --> 00:12:32.280 William Cheng: Okay, because all the addresses inside these data file. They're all we locatable addresses that. So later on the link or watch to figure out what the addresses, then 95 00:12:32.910 --> 00:12:42.360 William Cheng: And then determine their the final address now is over here says LD is the linker linker is that invoke to combine them into program so linker knows where preface. 96 00:12:42.690 --> 00:12:52.650 William Cheng: This code over here, you know, we don't have print out right here, but we call the function Pranav, so it's the job of LD, which is the linker to find the code for print and copy that into the executable program. Yeah. 97 00:12:54.930 --> 00:12:59.370 William Cheng: Alright, so how does that the linker over here knows what needs to be done. 98 00:12:59.910 --> 00:13:02.520 William Cheng: Okay. So as it turns out what the linker will do is it will take all the 99 00:13:02.820 --> 00:13:10.620 William Cheng: Files together. So what's inside of that file right so inside a Docker file so that that was basically their data structures for the linker to to to to parse 100 00:13:10.890 --> 00:13:16.500 William Cheng: Just like you know like 111 and two with parts a text file, the job of the link or here's the parcels binary file called 101 00:13:16.800 --> 00:13:26.310 William Cheng: Files and then what we'll do is that it will try to sort of figure out how big is your data segment how being to take segment and then it will take multiple files over here, merge them altogether to figure out what 102 00:13:26.940 --> 00:13:34.170 William Cheng: To figure out every address. OK. So again, what's inside of the inside of that file or just data structure that describe 103 00:13:34.710 --> 00:13:44.340 William Cheng: You know what these memory segments supposed to look like. Okay, so the link or all of those is combine them all together in the end to end up with one text admin. And one day the second at 1pm segment. Okay. 104 00:13:46.050 --> 00:13:46.470 William Cheng: All right. 105 00:13:50.040 --> 00:13:58.680 William Cheng: Alright then eventually the linker will create the executable file. Yeah. So, therefore, these are the must contain a list of external symbols, you know, 106 00:13:59.820 --> 00:14:05.700 William Cheng: So, so, so far, they actually have a lot of detailed information about every symbol, you know, inside this program. 107 00:14:06.300 --> 00:14:14.760 William Cheng: You know, what is the function look like you know how big is the function and what kind of argument doesn't return you know how many argument does it take all that information is going to get stored inside of that file. 108 00:14:15.270 --> 00:14:24.600 William Cheng: Okay, so, so, so, so the linker can use that information in order to generate the executable program. Yeah. So we're going to see some detailed by exactly how this is done. Yeah. 109 00:14:25.440 --> 00:14:34.080 William Cheng: So when you compound mean Darcy into main that oh there's an intermediate step called main s. So this is going to be the assembly code we show you, someone something called already so we're not going to 110 00:14:34.470 --> 00:14:40.410 William Cheng: Talk about that anymore. Right, we're going to look at the new stuff on this page. Okay. So we see that, you know, for our code over here. 111 00:14:40.800 --> 00:14:49.440 William Cheng: You know why we're actually we show a program offset for all the variable be here. Okay, so these will be the temporary address all these variables going to get 112 00:14:49.890 --> 00:14:56.220 William Cheng: There and then we saw this before we hear this is the operation, the operation code, you know, for the Intel x86 CPU. 113 00:14:56.430 --> 00:15:03.690 William Cheng: And then over here, these are the argument for this operation co right we saw stuff like this before. And now I'm going to pay attention to all the other stuff on the screen. 114 00:15:04.170 --> 00:15:10.620 William Cheng: That alright so over here. There's a bunch of a thing that started with a period. So those are called assembler directors. 115 00:15:10.890 --> 00:15:18.510 William Cheng: There. So what you do is that you take your Darcy file compile them into that as file and then you invoke the assembler and the assembler over here. We'll take that that 116 00:15:18.900 --> 00:15:26.400 William Cheng: You know that that is file and then they will actually assemble them into data files. Okay, so we're going to see some of these sort of assemble a directive. 117 00:15:26.760 --> 00:15:30.900 William Cheng: To see what they are guys will get the corresponding code over here is on the right hand side over here. 118 00:15:31.080 --> 00:15:38.850 William Cheng: And this is going to be the assembly code that you will get generated in addition to the regular assembly code and now they also contain a bunch of assembler directors. 119 00:15:39.660 --> 00:15:48.780 William Cheng: Okay, so for example over here says that data. That means that here is the beginning of the data segment. Okay, so you should sort of think about is that instead of that over here at 120 00:15:49.530 --> 00:15:56.550 William Cheng: The top of our content a text admin. They also contain a data segment. And also, therefore, they also contain the PSS segment. 121 00:15:57.630 --> 00:16:03.240 William Cheng: Okay, so instead of that over, over here, they're all divided into three different segment tech the tech segment is what has a cold over here, right. 122 00:16:03.450 --> 00:16:13.560 William Cheng: The data side menu. So if you look at this about see that as a thought over here. Once you go into the data segment right initialize global variable, going to the other side, man. So as over here should be inside the data center. 123 00:16:14.490 --> 00:16:23.580 William Cheng: Okay, so, so, so we have the here's a, here's the beginning of the data side man and at the data. So that means that our start and memory location zero. So again, this is a temporary memory location. 124 00:16:24.000 --> 00:16:34.500 William Cheng: Yeah. And then if you look down a little bit over here it says Ted's over here is that here is that that texts like Matt. So you can actually draw a line right here right above the lobby, here's the data segment and below this over here. So tech segment. 125 00:16:35.670 --> 00:16:40.380 William Cheng: Okay. So, okay, you're done. Oh, five divided into multiple segments data and text in this case. Yeah. 126 00:16:41.910 --> 00:16:42.240 William Cheng: All right. 127 00:16:42.570 --> 00:16:49.860 William Cheng: The opposite over here, they all start at zero, right. So, you know, so your program starts at memory location zero your data also started memory location zero 128 00:16:49.950 --> 00:16:59.820 William Cheng: So they actually they said exactly the same memory location. But in this case is ok because these are temporary memory at the other temporary or justice. So later on the link or actually figure out what the real addresses are 129 00:17:00.180 --> 00:17:09.690 William Cheng: Okay, so therefore it's probably okay for both of them to be zero. Yeah. So all these memories segment. They're all real locatable memory segment right so then the link or we'll figure out the final address 130 00:17:10.500 --> 00:17:16.800 William Cheng: Right. So there's about global director over here to tell you which symbol needs to be export it outside of this module. 131 00:17:17.280 --> 00:17:27.870 William Cheng: OK. So again, we wish we have two modules Mendes is going to combine two main oh and sabar Darcy going to combine this episode. Oh, so I have two monitors over here every module can have export. 132 00:17:28.380 --> 00:17:35.940 William Cheng: The symbols. So the experts involved, meaning that other module can actually use all these symbols, guys. Okay. If you look at the secret. The secret over here. 133 00:17:36.420 --> 00:17:47.250 William Cheng: Which symbols are exploited. Okay. So if you look at the secret over here as over here is a global variable, so therefore it's you know it's going to be exploited and also the main function over here is also 134 00:17:47.700 --> 00:17:49.800 William Cheng: The global variable. So, therefore, it needs to be export. 135 00:17:50.130 --> 00:17:56.790 William Cheng: So you can see that over here says inside the data segment. There's a symbol call as and that's a global symbol. So we'll get to sitting right here, over here. 136 00:17:57.030 --> 00:18:01.410 William Cheng: And inside the tech stack. Man, there's a global simple command over here and needs to be exploited. 137 00:18:01.920 --> 00:18:07.770 William Cheng: OK. So again, the reason that we know that these they need to be export it because they don't have the T word static in front of it. 138 00:18:08.160 --> 00:18:13.260 William Cheng: Okay, so if you have a static global rebel or static function. That means that those symbols are not export it. 139 00:18:13.770 --> 00:18:22.500 William Cheng: Was okay I keep using the word symbol. What's a symbol right symbol is something that has an address. Okay, so your function has an address your variable has an address all these things are symbols. 140 00:18:23.220 --> 00:18:37.080 William Cheng: OK, so the symbol in this case will be a x and and and may over here will be a symbol. So again, what is the function we sort of call the function pointer. But in the end, all we are manipulating over here are addresses. Okay. So, therefore, every symbol gets an address. Yeah. 141 00:18:38.640 --> 00:18:46.290 William Cheng: All right, what else is here, we here. So as over here as over here is four bytes law. How do we do this for by Islam, because it's an integer pointer. 142 00:18:46.530 --> 00:18:58.050 William Cheng: There so again for Intel long or else. I mean, that means it's for by as long or as short as Dubai's long and a BS one by law. So in this case over here starting at address zero inside the data segment. 143 00:18:58.260 --> 00:19:07.920 William Cheng: We're going to have a four by number and the name of the symbol over here is going to call a sign on for by numbers over here and the content of this memory location is going to be the symbol x 144 00:19:09.360 --> 00:19:19.800 William Cheng: Okay. So in this case, as is for by the or is it starting a memory location zero over here and then swap is law. So therefore, over here, said that law, and we're going to put the value of x years. So what is the value of x here. 145 00:19:20.190 --> 00:19:31.440 William Cheng: So remember, x is just a symbol. So x over here actually refer to the address of x. Okay. So, this one doesn't mean that go to the memory location or has that and then copy data from, you know, find that memory location that 146 00:19:33.150 --> 00:19:42.420 William Cheng: Into this memory location 00 okay because in order for you to do that to copy data for memory location zoo and put it right here. You have to execute C code. 147 00:19:44.310 --> 00:19:49.350 William Cheng: Okay, so that's why I will be here. We don't actually get any sequel. Right. We only say I will put x over here. So x is the address of x. 148 00:19:49.530 --> 00:19:54.780 William Cheng: And then the, the compiler and the link or they can actually sort of figure out what the address have access at this time. We don't know what 149 00:19:55.050 --> 00:19:59.370 William Cheng: The address of access, because x is that external integer. So therefore, we don't know what it is. 150 00:19:59.670 --> 00:20:05.820 William Cheng: Right. So again, we're just going to put a temporary address over here. Maybe equal to zero. Maybe we'll do some some any, any number you want doesn't really matter. 151 00:20:05.970 --> 00:20:20.580 William Cheng: Because they they're all going to replace it. You know what we perform we location. We're going to ask you all guys should put in the real value that right. So this guy's X remain unresolved, because x is the external external external integer 152 00:20:23.460 --> 00:20:29.010 William Cheng: Over here, it says these three locations require these three places require relocation. Okay. 153 00:20:29.190 --> 00:20:38.970 William Cheng: The first area over here point you to the address of x, right at this point we don't know what address have access. So later on the link or need to figure that out. So one thing that we need to do is that will be right into the 154 00:20:39.270 --> 00:20:50.760 William Cheng: File. We're going to say that right here. There are some unresolved symbol and we're going to tell the linker to say, hey, later on when you try to generate the executable file. Make sure you put the address of the real x into this memory location. 155 00:20:51.510 --> 00:20:57.090 William Cheng: Okay, so that will be a relocation instruction that the linker will be up to us and we're going to store that in the data file. 156 00:20:57.450 --> 00:21:04.050 William Cheng: Then the second one over here is that, is that a cut over here. There's. So again, if we look at instruction over here says move out as 157 00:21:04.530 --> 00:21:17.640 William Cheng: So what does as well as we don't know what it is right now. Right, right. I said location zero as location zero. So later on as when it gets a final address. We also need to come in over here and patch the executable code over here. So now we're going to put the 158 00:21:18.030 --> 00:21:19.560 William Cheng: The real address for me right here. 159 00:21:20.730 --> 00:21:28.260 William Cheng: Okay. So, at this time, you know, where is this co write this code is actually. So if you look at this quarter were very heavily this code over here, started you know 160 00:21:29.010 --> 00:21:34.110 William Cheng: saucers and memory location six and the next instructions over here. So if the memory location you love it. 161 00:21:34.950 --> 00:21:49.800 William Cheng: Okay, so see you have a minus 64 to five. So this one is a fight, fight instruction. Okay, the first bite is the consortium move house or something, comma, yes. And the next four by is going to be the absolute address of the of the symbol x 162 00:21:50.880 --> 00:21:56.580 William Cheng: Yeah. So in this case, there is a five by instruction there the first bite is the operation co for for this particular charger. 163 00:21:56.790 --> 00:22:06.120 William Cheng: And the next four by is going to be the absolute address of x guys are air travel is equal to zero, right. So in this case, the relocation instruction is going to be saying something like this. 164 00:22:06.600 --> 00:22:19.380 William Cheng: You know, well what once you figure out all these you know the the the addresses. What you need to do is that you need to come to the text that bell go to offset seven and then put the actual address of the global variable x right here. 165 00:22:20.160 --> 00:22:27.630 William Cheng: Okay, so that's what you need to do, put it put out inside your relocation instruction. So the linker can figure out exactly what to do. Okay. 166 00:22:28.650 --> 00:22:34.770 William Cheng: So, so what is this called correspond to over here, right, it seems, it seems like this, right, y equals to start X over here. So again, 167 00:22:34.980 --> 00:22:42.000 William Cheng: This you know code over here. Get component to assembly language over here. So at this point we actually, we need to know the address of as then 168 00:22:42.600 --> 00:22:48.870 William Cheng: The last one will be as cost bar right at this time. Where's the bar right we have no idea what's up is because so far is undefined simple 169 00:22:49.110 --> 00:22:56.490 William Cheng: Okay, so in this case again inside the relocation, you start, you have to say that, you know, again, this is the file by instruction in the first by said this is the core machine shops, you 170 00:22:56.760 --> 00:23:04.950 William Cheng: Followed by the four bytes of address for sabar. Okay. So, therefore, again, the relocation instruction will say inside the tech segment and offset 20 171 00:23:05.250 --> 00:23:14.610 William Cheng: Okay, we need to put the address of sidebar, you know, in this memory location. Once we figured out exactly what value is somebody what what values of our symbol, but 172 00:23:15.960 --> 00:23:24.300 William Cheng: As it turns out, the incomes, a little weird, you know, when they make a function called over here. They can't really put the absolute address in that memory location. Okay. The example that we saw. 173 00:23:24.870 --> 00:23:30.210 William Cheng: So so so before we hear so you're all the places over here, we need to perform relocation. 174 00:23:30.540 --> 00:23:35.430 William Cheng: So we here, we need to put the absolute address of x, right here, right at that offset zero and 175 00:23:35.730 --> 00:23:47.100 William Cheng: Into the tech side in the data side, man. And also, I will need to put the absolute address of x into offset seven into our tech stack, man. But for the summer. Over here, we need to put a different kind of address 176 00:23:47.580 --> 00:23:58.110 William Cheng: Okay, and that's just one thing that's weird about Intel as for Intel what they need to do is I need to put something called the PC relative he sees this program counter, which is the instruction pointer or the IP. 177 00:23:58.410 --> 00:24:10.020 William Cheng: So this call needs to be a PC relative call. So what do you need to do is that you need to find the address of a sidebar and then subtract the address bar by the memory location for this instruction and take the difference and put it right here. 178 00:24:11.100 --> 00:24:14.310 William Cheng: Okay, why the Intel do this. I have no idea. But that's, that's how we tell this 179 00:24:14.700 --> 00:24:22.740 William Cheng: Okay, so therefore again inside the the executable inside of that file, you need to leave instruction for the linker to be very, very precise. 180 00:24:22.920 --> 00:24:32.430 William Cheng: For these two memory location, you need to say put the absolute address of x and x right there for somebody to say I want to put the PC relative address in a location 20 181 00:24:33.780 --> 00:24:36.480 William Cheng: Okay, so this way, the linker will know what to do. Yeah. 182 00:24:38.280 --> 00:24:45.300 William Cheng: Alright, so that's some of the highlight of the main function over here. So the next thing that we're gonna do is, I'm going to look at some bar somewhere I look like this over here. 183 00:24:45.660 --> 00:24:54.450 William Cheng: You know, over here we have a global grab over here that's x x is on the initialize so therefore, x needs to go into the BSS segment and not go into the tech 184 00:24:55.140 --> 00:25:02.580 William Cheng: data segment that and also there's a printer function. We don't know what print Fs. Right. And also there's a string over here as a wish of the string go there. 185 00:25:03.510 --> 00:25:15.540 William Cheng: So over here we can see that, you know, as it turns out, the string over here. It's going to go into the data segment, guys. So this stream over here is going to be a data center. Why does it go to the data segment what because because the string is to initialize global variable. 186 00:25:16.620 --> 00:25:22.950 William Cheng: Okay, so what is the name of this global variable why in this case over here. It's a compiler generate a name because because the string doesn't have a name. 187 00:25:23.340 --> 00:25:28.200 William Cheng: Right, so one of his other compound is going to generate an A. So in this example over here. This one is called print f arc 188 00:25:28.740 --> 00:25:36.000 William Cheng: In reality, you know the name of compiler generators, but probably going to call data one data to data three or something like that. Right. This example is going to call prenup are 189 00:25:36.360 --> 00:25:42.510 William Cheng: Okay, so what do you would do is that it will place the street at offset zero into the data segment. Again, this is real locatable address 190 00:25:42.750 --> 00:25:53.250 William Cheng: And then this dream, how many bytes is the string right 123456 and then that's it. And the sub and backside zero. So all see string zero terminate. So therefore you need a vice law. 191 00:25:53.940 --> 00:26:04.050 William Cheng: Okay, so put up, I'll go over here to start as location zoo of the data segment and they use a doc string right here. That's the here is a see string. So therefore, in the end, what time they were backside zero 192 00:26:04.500 --> 00:26:14.910 William Cheng: Okay, so you can actually see that the next available memory location is that it did is I went over here is memory location zero memory location age. So, therefore, this string is exactly eight by slow and not seven vice long 193 00:26:15.810 --> 00:26:24.030 William Cheng: That. So again, this example here, you know, the, the, the, the string constant actually goes into the data segment. Okay. 194 00:26:25.350 --> 00:26:28.590 William Cheng: So this particular students over here is us over here, what you call print up so 195 00:26:29.010 --> 00:26:33.810 William Cheng: Once a week or printer. Right. So again, whenever you make a function call. We saw that before you at the beginning of chapter three. 196 00:26:33.990 --> 00:26:39.930 William Cheng: You start pushing did on to the stock from the right to the left, right. So over here, we're going to take the value of I'm going to push it onto the 197 00:26:40.170 --> 00:26:43.680 William Cheng: onto the stack, we saw how that's done. But what about a string constant over here. 198 00:26:44.070 --> 00:26:49.560 William Cheng: Right. So again, the string constant over here is just a memory location and that will be offset zero in the data segment. 199 00:26:49.890 --> 00:27:00.420 William Cheng: Again, it's a real credible address so be here when I say push out with $1 print apart. Right. So again, dollar over here. It's going to be a numerical concept so dollar printer art is exactly zero right here. 200 00:27:00.840 --> 00:27:06.660 William Cheng: Okay so Libra. We also got to say that this. The other, the code right here, over here needs to be a real locatable address. Okay. 201 00:27:08.640 --> 00:27:11.760 William Cheng: All right. Okay. What else is here. 202 00:27:13.080 --> 00:27:22.140 William Cheng: Okay, so we're here and the symbols over here. So, x over here. It's known as a common symbol, because you can actually have multiple, you know, multiple 203 00:27:23.010 --> 00:27:27.720 William Cheng: Files, all of them will contain the same x ray. So in this case, the X over here will be sure 204 00:27:27.990 --> 00:27:38.760 William Cheng: There's over here says there's a common segment that the variable extra go into so so here's the name of the symbol. This one sitting and location zero of the BSS segment. 205 00:27:39.090 --> 00:27:42.090 William Cheng: Okay. And so I'll be here since it's common so therefore it's also export it. 206 00:27:42.870 --> 00:27:53.550 William Cheng: Okay, so come over you actually mentioned two things. Number one is that this symbol is export out because over here. We don't say static, right, so that was the symbols export it. And number two is that this particular symbol is a global symbol. 207 00:27:54.090 --> 00:28:05.580 William Cheng: Okay, so you use the.com directive over here and then x over here. How big is that right, since you don't initialize me need to say how big it is over here is that this one is four bytes law. And that's why the next available memory location over here is equal to four. 208 00:28:06.180 --> 00:28:13.800 William Cheng: OK. So again, this is how you declare an initial as global variable, okay, using a.com directive so that it can be shared among multiple 209 00:28:14.910 --> 00:28:16.050 William Cheng: Multiple memory segments. 210 00:28:18.480 --> 00:28:25.800 William Cheng: Sub over here. Again, it's not a steady function, so therefore it's a global variable here. So again, we need to export the symbol using the global a directive. 211 00:28:26.340 --> 00:28:33.690 William Cheng: So in this example over here relocation need to be done for the print apart because rhinos locations zero instead of data segment. So again, this is real credible address 212 00:28:33.930 --> 00:28:39.180 William Cheng: And we call it the printer function over here. At this point, we have no idea what print FS it's undefined right here. 213 00:28:39.660 --> 00:28:50.490 William Cheng: So they have all we need to do is that, again, we need to make a note inside our data file to say that, you know, the link or need to actually figure out what the addresses and then put a PC relative address will print out, put it right here. 214 00:28:50.850 --> 00:28:54.960 William Cheng: So, so, so, so right in the end you will regenerate the executable file. Yeah. 215 00:28:57.120 --> 00:29:08.460 William Cheng: Alright, so, so again you know what's inside the object fall over here is every object file right so here's a thought oh file right the way you think about is that is divided into three different segments. There's a data segment. There's a BSS back then. 216 00:29:08.730 --> 00:29:09.780 William Cheng: And there's a tech segment. 217 00:29:10.140 --> 00:29:20.040 William Cheng: Okay. And is that every segment. There's data in there. And also there's a bunch of symbol in there. And then, and the symbols are the know that the name all these variables and every symbol is going to get a temporary address 218 00:29:20.250 --> 00:29:23.310 William Cheng: And the linker is going to sort of fall you fall down there was there was sort of figure out 219 00:29:23.760 --> 00:29:27.210 William Cheng: You know, what's the final dress. Okay, so if you think about it, if you have two different 220 00:29:27.540 --> 00:29:35.370 William Cheng: Our one is the main.oh and the otherwise the sub sub par dot over here, this also have data segment. There's also a piece he has this segment is all I have is tech stack, man. 221 00:29:35.580 --> 00:29:45.420 William Cheng: So what the wrinkle will do is that they will take all the data segments over here and combine them into one giant data segment that will include the one from me. Oh and also the one from Pardo 222 00:29:45.960 --> 00:29:52.650 William Cheng: Okay, so this way, we'll figure out exactly how big the data segment is for your final program right by combining all the 223 00:29:53.100 --> 00:29:54.780 William Cheng: Bad combining all the data segment together. 224 00:29:55.110 --> 00:29:57.780 William Cheng: Yeah, and also for the, you know, for, for the PSA segment. 225 00:29:57.990 --> 00:30:04.440 William Cheng: The link over here also need to sort of figure out sometimes the same symbol is using multiple modules. So there was trying to see which one is conflict. 226 00:30:04.590 --> 00:30:12.750 William Cheng: And they will merge them into one PSS like men. Right. So once you figure out what the BSS admin, is it can actually figure out how big it is and get give every symbol. 227 00:30:13.680 --> 00:30:23.670 William Cheng: You get every symbol, a global address. Okay. And then the link will also have to do is to take all the the text document over there together combine them into one big tech segment at the top over here. 228 00:30:24.000 --> 00:30:32.130 William Cheng: That. So this text. I've been over here. Again, it's a concatenation of all the other tech segment over here so that every function over here will find it finally get a final address 229 00:30:33.570 --> 00:30:41.250 William Cheng: Okay, so, so therefore, instead of auto fall over here. They need to have all that information for them to feel you know fulfill the link or to figure out how to build the final address face. 230 00:30:41.430 --> 00:30:48.840 William Cheng: It also need to sort of figure out what are the symbols that are global, one of the undefined symbols. So later on the link or can actually perform address 231 00:30:50.160 --> 00:30:51.990 William Cheng: So, so, nigga perform a relocation. 232 00:30:53.850 --> 00:30:56.280 William Cheng: Also, do you need to perform symbol resolution, but 233 00:30:57.420 --> 00:31:08.070 William Cheng: It also contains instructions for relocation and they are very, very specific tell the link or exactly what to do, put at put absolutely address here please piece relative address here. So everything can be figured out 234 00:31:08.550 --> 00:31:14.820 William Cheng: Okay, so again the link of those laws that we're not going to sort of go over everything. The link or does right so it'll give you an idea what actually do that. 235 00:31:15.270 --> 00:31:28.080 William Cheng: Alright, so, so in the end it will generate object file. So for Unix and Linux system. There are two ways to look at the object. Wow. One is to use a program called N N and M stands for mnemonics guys. So what do we do, is that it would take it down. Oh file will take 236 00:31:29.370 --> 00:31:35.340 William Cheng: You can also take an executable file. And what do we do, is that it will print you the symbol table for this for this. 237 00:31:35.670 --> 00:31:41.220 William Cheng: Hour for the executable file. Okay, what is the symbol table right. Remember the text segments over here is full of symbols. 238 00:31:41.400 --> 00:31:48.150 William Cheng: Every symbol has an address right so printing out a symbol table, meaning that for every symbol, it will tell you what is the final address, then 239 00:31:48.270 --> 00:31:58.140 William Cheng: Similarly, there's going to be a lot of symbol in the data side man for every symbol. It was a pre you what the final address this in the BSS segment over here. Again, there's a bunch of addresses over here and they will print it out what what the 240 00:31:58.620 --> 00:32:06.750 William Cheng: What the final addresses. Okay, so that's why. And I'm does and what about the other members admin over here at the dynamic seven and then and the stack segment. 241 00:32:07.560 --> 00:32:11.190 William Cheng: They don't need to go into the executable file because and consumable 242 00:32:12.090 --> 00:32:23.460 William Cheng: Because when you progress starts to run, and then at that time, it will need the, the, the, the heap segment at the stack segment okay when you'll put up and running. You don't even need those in order for you to fill those segment you need to execute code. 243 00:32:24.510 --> 00:32:30.510 William Cheng: Okay, so that's why they are not part of the executable file. Okay, the XML file only have tech segment the data segment BSS 244 00:32:31.470 --> 00:32:36.660 William Cheng: Right. The other program that you can run that was actually show you the detailed information of the executable file. 245 00:32:37.050 --> 00:32:44.550 William Cheng: The program is called object dump. So when you are working on Colonel three, you're going to get familiar with object dumb because you need a lot more detail, you know, again, 246 00:32:45.480 --> 00:32:50.790 William Cheng: In order for you to do. Colonel three. Okay, so at that point to get familiar with it. So right now I'm just gonna say that. 247 00:32:50.970 --> 00:32:58.110 William Cheng: Object done is going to generate a lot of information for you executable file. Okay. You can do men object dumb and you will see that goes on forever. Okay. 248 00:32:58.620 --> 00:33:04.530 William Cheng: If you do a man or and then you will see that's actually pretty short because all it because all it does is a printer symbol table right 249 00:33:08.250 --> 00:33:11.490 William Cheng: I think the textbook. They sort of show you. You know what this 250 00:33:15.120 --> 00:33:24.450 William Cheng: You know, so, so the textbooks are to show you what sabar look like I just want to make a note over here said no tool can generate exactly about print out 251 00:33:24.810 --> 00:33:33.450 William Cheng: Okay, so I guess, you know, so, so, so I guess, Professor OB nurses, take this up our fall over here and just sort of you know type out all this information to show you what actually goes into the 252 00:33:34.320 --> 00:33:44.700 William Cheng: Guys that again no program can generate this right. Some people keep trying to generate this for now and there's no way to do it. Okay, and then doesn't do that object doesn't do that. So we are going to sort of review what's inside of that file. 253 00:33:46.200 --> 00:33:54.660 William Cheng: Alright, so do we need to come out. Connect the assembly code over here with the secret over here and also with the dump of the the the the the the 254 00:33:55.350 --> 00:34:06.990 William Cheng: Phone over here. So the relocation over here is required for a print up over here and print up. Right. So again, print up over here is going to sitting at memory location seven. Is that a tech stack, man. 255 00:34:07.260 --> 00:34:11.070 William Cheng: And then print function over here is sitting at memory location. Well, 256 00:34:11.850 --> 00:34:19.260 William Cheng: You know, instead of tech segment. Okay. So, therefore, you know what we actually do this dump a sidebar that oh, we're going to see that inside of tech segment. 257 00:34:19.500 --> 00:34:30.540 William Cheng: For the tech segment. There are two places required relocation, what is it offsets seven right over here, offset seven right here and the otherwise that offset 12 is right here. Okay, so they will tell you how many bikes. 258 00:34:30.750 --> 00:34:41.700 William Cheng: You know that there's a link or has to patch out. So in this case, both in our fulbright's law and the value over here. One of them is going to be the the address of print Fr. And the other one is going to be the PC relative address will print up 259 00:34:42.630 --> 00:34:45.930 William Cheng: Okay, so it turns out again in tell us a little weird I for for, you know, for 260 00:34:46.530 --> 00:34:55.710 William Cheng: For sub over here, it actually require a PC relative address before print of arc. As it turns out, you know. So what happened is that they might be sitting in a different different segments. 261 00:34:56.550 --> 00:35:08.940 William Cheng: You know, because again, Intel's weird. So in this case, what it will do is that it will put out actually that the address bar. Okay, so this way. The, the code that Assembly, the machine code that gets generated actually different for different kinds of function call. 262 00:35:09.990 --> 00:35:22.680 William Cheng: Okay so anyways we're going to just take this as a face value and then trust the textbook and say that this is actually correct. Okay. Without asking say exactly why would the compiler actually generate different kinds of call one for print up at the other level so far. Okay. So anyways, 263 00:35:25.740 --> 00:35:26.910 William Cheng: Sorry, over here, right. 264 00:35:28.290 --> 00:35:38.190 William Cheng: Oh. Oh. Sorry. I think I made a mistake here. So also seven over here is the address of the printer are not the printer function. Okay. So over here, this is the actual address 265 00:35:38.610 --> 00:35:44.850 William Cheng: Right. As I mentioned before, this is the symbol right now is equal to value of zero. And that's a global variable. So in this case, of course, you need to actually address 266 00:35:45.450 --> 00:35:53.820 William Cheng: Over here, we need to make a function call. So the function got over here, we have to use the PC relative address so, so I think Intel whenever you make a function called you need a PCI relative address 267 00:35:54.000 --> 00:36:01.050 William Cheng: But when you try to access a global variable in this case you need the actual absolutely address. Okay, so, yeah. They're there for the different sorry I made a mistake there. 268 00:36:02.790 --> 00:36:15.510 William Cheng: Alright, so again, you know, is that a dot O Father wants to tell you what kind of symbols are export it right over here says global X over here and what is saying over here is that inside a PSA segment offset zero. There's a symbol call eggs, and this is a symbol that get 269 00:36:16.110 --> 00:36:26.130 William Cheng: Export add inside of tech segment over here to offset zero. That's the function sidebar. This one also needs to be exploited. Right. So again, you could look at x, you can sell bar, they don't have the key word static in them. 270 00:36:26.550 --> 00:36:29.370 William Cheng: So therefore, they are the global variables that 271 00:36:29.880 --> 00:36:39.780 William Cheng: For me, that ass over here rise over here. There are two places. There are there are resolved, right. So, so we can also look at the C code over here, we should know right away that which symbol is unresolved. 272 00:36:40.050 --> 00:36:44.850 William Cheng: X, we don't know where it come from i. So, therefore, x zone resolved will be here a sidebar over here. We also don't know where it is. 273 00:36:45.360 --> 00:36:52.680 William Cheng: So inside text inside the data segment over here offset zero. There's a symbol and that's for by law. We need to put the actual addresses there. 274 00:36:52.860 --> 00:36:59.580 William Cheng: And also in the dataset in the in the tech segment over here offset 20 we need to put the PC rather have address of sidebar right there. 275 00:36:59.910 --> 00:37:12.750 William Cheng: Right. So again, if you look at a main dot O in without over here, it will say that, you know, the symbol. So here's undefined sub oh here's on the by, and also for the relocation sorry we know relocation instruction over here it says it 276 00:37:13.800 --> 00:37:23.730 William Cheng: Says over here says relocation instruction was symbol. And so we here at offset zero. There's four bytes. Over here I would need the absolute address of the variable x. We need to put it right here and then 277 00:37:25.110 --> 00:37:36.390 William Cheng: Over here, and also 20 right off the 20 over here, we need a PC relative address bar right here. Okay. And also, I guess there's three places over here, we also need as right here so address seven 278 00:37:37.050 --> 00:37:42.180 William Cheng: Your address seven over here, we have four bytes. We need the absolute address them as we need to put in there. Yeah. 279 00:37:43.800 --> 00:37:54.480 William Cheng: All right. It's a sub over here. They also symbol that's the the prenup is undefined. So again, if you link the sandbox or Sabato and make that all together. You're going to know that this particular symbol doesn't exist. 280 00:37:54.780 --> 00:38:03.360 William Cheng: Okay, the linker is going to provide is that it's gonna, it's going to do symbol resolution by looking for this symbol inside the seed library. 281 00:38:03.720 --> 00:38:18.210 William Cheng: Okay, so what's inside to see library right see library is live. See a that's a C library over here. A dot a file is a library for that a file is simply a concatenation of a bunch of files. Okay. So you said this library. There's gonna be a bunch of that office. 282 00:38:19.530 --> 00:38:26.370 William Cheng: They're actually they're combined together to create a library file that. So in this case, when we find printer. If I were going to find print up in another 283 00:38:26.640 --> 00:38:33.840 William Cheng: File. So in this example, we are going to say, well, there's a phone call print off that. Oh, and that's part of the C library. So this is what the printer that oh my look like 284 00:38:34.350 --> 00:38:44.760 William Cheng: Okay. He has a data segment that the sizes 10 2040 has a PSA segment size of 256 as a tech segment. As it turns out, putting up is a really big function. Right. The example that we saw before. 285 00:38:45.240 --> 00:38:57.510 William Cheng: sabar is only 24 bytes long. What about me means only 36 bytes. All right. So again, these segments are actually pretty small. When you go to the printer. The printer, but that over here, you can see that this is a much bigger. 286 00:38:58.530 --> 00:39:02.400 William Cheng: Much bigger function because printer. When you look at the main page, you will see that it's very, very big 287 00:39:03.060 --> 00:39:13.770 William Cheng: Okay, so it's complicated. There's a global symbol called printer. If sitting offside 100 ago we locatable address over here and also printer has an undefined symbol. It's the racism call 288 00:39:14.490 --> 00:39:20.250 William Cheng: Right, so that should make sense, right, because printer is a rapper function around the rice system called over here. So in this case, again, 289 00:39:20.760 --> 00:39:29.790 William Cheng: Yo, yo, your linker. You know what, what he tried to copy the code from print up into executable functioning again he to copy the data segment the psi when the tech segment. 290 00:39:30.000 --> 00:39:38.640 William Cheng: And when you copy the text document over here. They also know that while there's a there's racism call that's on the buy. And now, again you will go to the seed library, try to look for. Right. So maybe the right 291 00:39:38.850 --> 00:39:43.830 William Cheng: Is inside of our call right over here, it will look like this that data segment over here is empty, right, so remember 292 00:39:44.220 --> 00:39:48.060 William Cheng: The racism call every system cards. A thin wrapper around the track machine instruction. 293 00:39:48.600 --> 00:40:00.720 William Cheng: Okay. So, therefore, you shouldn't be surprised that that data segment over here is empty. As it turns out, a PSA second over here is not empty, and this is equal to four. There's a global grab Boko Error number and then the city and local memory location zero 294 00:40:01.470 --> 00:40:08.670 William Cheng: That. And the reason for that is that where you could make the rice system call it the right system will call fail inside the racism, call it was set up the air number 295 00:40:09.240 --> 00:40:20.220 William Cheng: Okay, so therefore, again, it makes perfect sense that in writer. Oh, it required for bites inside the BSS like man, and this symbol of year is going to be shared by all the system call functions. All this isn't called that all files over here. 296 00:40:20.610 --> 00:40:30.480 William Cheng: You know, because they all want to share exactly the same number number global variable. Then finally the tech segment for the right system called over here, you can see that it's very tiny is 16 vice law. 297 00:40:31.290 --> 00:40:33.990 William Cheng: Okay. So, by the way over here. You know, you know, every segment over here. 298 00:40:34.800 --> 00:40:41.940 William Cheng: Has a content right the data segment over here as a bunch of global variable. And I think I skipped this one before I over here is the content of the data segment. 299 00:40:42.180 --> 00:40:48.900 William Cheng: Remember inside the data segment. They are initialized global variable. So over here are the initial value for the entire data segment. 300 00:40:49.380 --> 00:40:55.050 William Cheng: That all data segments that is that over the BSS over here. We don't have contents because they're just a bunch of symbols. 301 00:40:55.380 --> 00:41:06.360 William Cheng: Because there are initialized global global yeah the tech segment. What's the content Remax admin. Well, those are the machine code that get compile beers are the contents of you is going to be the machine instruction that get compiled from the assembly code. 302 00:41:07.050 --> 00:41:12.600 William Cheng: Yeah, so the right over here is 16 bites of assembly call again. It's a tiny function because it's just a system called so 303 00:41:14.100 --> 00:41:20.910 William Cheng: It's a thin wrapper around the track machine structure that right putting all these things together. What else do you name. 304 00:41:21.540 --> 00:41:28.800 William Cheng: Well, so, you know, one of the function that you don't. Right. It's a startup routine registered beauty is the one that cost me and then take the return value man called X and on it. 305 00:41:29.010 --> 00:41:36.150 William Cheng: Right. So over here we also have a standard routine. The data segment of here is equal to zero. There's no PSS the size over here is pretty small. 306 00:41:36.630 --> 00:41:41.190 William Cheng: So in this case, there's an undefined symbol for this function which is making right because the assumption is 307 00:41:41.460 --> 00:41:49.380 William Cheng: Gonna call me at the time when you create the startup function. It doesn't know what it doesn't know where money is so therefore it's make perfect sense that may may is undefined symbols. 308 00:41:49.920 --> 00:41:54.540 William Cheng: So I think that the textbook. Also, if you don't make a mistake, another symbol, because it's going to be exit. 309 00:41:55.140 --> 00:41:58.890 William Cheng: Right, because what it does is that it costs me and take the return code and collected on it. 310 00:41:59.430 --> 00:42:06.690 William Cheng: Okay, and then the relocation over here is going to you know offset 21 there's the the sort of PC relative address will may, you know, etc. 311 00:42:06.960 --> 00:42:11.700 William Cheng: Now, and the content will be is going to be the machine instruction, you know, for the cell function calls me and that 312 00:42:12.240 --> 00:42:20.970 William Cheng: Is also right before it cost me, you need to set up the stack. So therefore, it takes a little more instruction to do that. And then, and then what it would do is that it will call them a function, but 313 00:42:22.170 --> 00:42:28.740 William Cheng: Alright, so, so, so the our example over here. The link, it will take all these information combine them all together and then again layout. 314 00:42:29.190 --> 00:42:36.420 William Cheng: You know, sort of lay out the address space. So again, what are they are the address space. The top part up here is going to be the text document, followed by the data, followed by PSS 315 00:42:36.630 --> 00:42:43.080 William Cheng: It doesn't have to be the this order the compiler and the link or they can decide what what they are just begun to look like. So in our example. 316 00:42:43.650 --> 00:42:48.870 William Cheng: You know, here are the symbol inside of text like man, here are the symbol and data segment here, the symbol inside BSS 317 00:42:49.590 --> 00:42:57.000 William Cheng: Well, so the link will be hidden law, the outer space and allocate memory over here. And as we mentioned before, there's something called pages. 318 00:42:57.390 --> 00:43:06.120 William Cheng: Okay. So as it turns out our Colonel typically what it will do is that it will use something called paging and they will allocate memory using the buddy system four kilobytes at a time. 319 00:43:06.720 --> 00:43:19.080 William Cheng: Okay. So, the link is aware of that. So what do we do that. It will also pen or the address space by allocating these memories segment. According to multiple, multiple pages. So therefore, every memory segments over here is going to get their own 320 00:43:20.100 --> 00:43:28.140 William Cheng: So, so, so, so this guy. Yeah, there's going to be a two different memory segment. What is the tech stack man that data and the PSS I've been a typically they're combined into one memory segment. 321 00:43:28.950 --> 00:43:37.590 William Cheng: Okay, so what do we do that or figure out how you know how big this memory segment is and then it will try to sort of figure out how many pages. Do you have to add. Okay, so you can actually fit all these things in them. 322 00:43:38.040 --> 00:43:46.920 William Cheng: Okay. So in our example because printer is a very, very large function right we saw before. It's 12,000 bytes law. So if you see that this is the first virtual address will print out as 4156 323 00:43:47.070 --> 00:43:53.910 William Cheng: You can see that the next virtual address over here for the racism called is 1615 says the difference over here is exactly 12,000 bytes. 324 00:43:54.660 --> 00:44:02.550 William Cheng: OK. So again, the understanding over here is that inside your tech segment you have all these functions. You just pack them, you sort of pack them together to be as closely as possible. 325 00:44:02.730 --> 00:44:08.790 William Cheng: So right after the main function immediately is going to be the sub or function right after summer summer fun. She's going to be the printer function right after 326 00:44:09.150 --> 00:44:16.410 William Cheng: The print function when I had the right function and etc. Okay, all these things going to be packed together as closely as possible. So they're all sitting right next to each other. 327 00:44:16.860 --> 00:44:24.720 William Cheng: Okay, so this way. We're going to save the address space. Yeah. As it turns out over here at the LD will allocate memory over here in different pages. 328 00:44:25.530 --> 00:44:33.480 William Cheng: That. So in that case, so often. So one of the one thing that compiler will do is that they will put the tech segment into this. So sorry. So 329 00:44:33.810 --> 00:44:42.540 William Cheng: That the entire tech segments over here into multiple pages over here. And then the next memory segment over here for the data and BSA they will they will have to start at a different page. 330 00:44:43.320 --> 00:44:53.880 William Cheng: Okay, so they will not put data and text and the text into the same page, right. So why is that right because over here is that the example that we saw over here the tech segment over here is going to end up using three different pages. 331 00:44:54.270 --> 00:44:59.190 William Cheng: The first one starts at four kilobytes. Right. And then the neckline is going to be eight kilobytes. And then 12 kilobytes over here. 332 00:45:01.020 --> 00:45:06.660 William Cheng: Then, and the next one over here is going to be 16 kilobytes over here. So the tech stack know will be occupy three different pages over here. 333 00:45:06.960 --> 00:45:15.390 William Cheng: Okay, so if the data segments starts immediately after the tech side. But over here, there's gonna be some incompatibilities right. What is the difference between data segment tech segment. 334 00:45:16.350 --> 00:45:18.270 William Cheng: What the tech segment and should be read only. 335 00:45:18.900 --> 00:45:27.000 William Cheng: Right, because we're program. Once you start running your program. You don't want your program to modify your program because then what kind of poker modify your own program. Well, it must be a virus. 336 00:45:27.600 --> 00:45:31.380 William Cheng: Okay. So, therefore, what you would do is that you will actually protect your tech segment by making a read only. 337 00:45:32.130 --> 00:45:38.370 William Cheng: Okay, so therefore the Davis. I've given the PSS like when they need to start at a different page boundary, because we know that inside 338 00:45:39.090 --> 00:45:49.680 William Cheng: Because the linker know that inside the Colonel, they will protect all these memory locations over here, a page at a time. Okay, so therefore the data and the BSS segment. They have to start a different different memory other 339 00:45:50.940 --> 00:45:52.320 William Cheng: They have to start a different page. 340 00:45:52.590 --> 00:46:04.590 William Cheng: That also, we see that the first page over here is actually not accessible study I location zero over here. We're going to reserve it for segmentation fall. So this way. Anytime you access memory location zero, we're going to make sure that this page is in 341 00:46:05.400 --> 00:46:13.800 William Cheng: This page as inaccessible. Okay. So, therefore, whenever you pull that refer to memory location zero, we're going to get a second digit fall. We're going to tap into the Colonel. The Colonel will kill your program. 342 00:46:14.640 --> 00:46:25.800 William Cheng: Okay. So, therefore, you know, since again the link or know that the operating system allocate memory and pages, they're going to reserve the first segment over here to be one page law and it's reserved for segmentation fall and the next 343 00:46:26.190 --> 00:46:35.190 William Cheng: Member segment over here is going to be for the tech stack, man. And in this example over here, it will be three pages long and the next segment over here is going to be for the data plus BSS segment over here. 344 00:46:35.430 --> 00:46:40.860 William Cheng: Okay. So in my example over here. Everything fits on one page. Okay. So, therefore, in that case, we only need one page. 345 00:46:42.060 --> 00:46:47.610 William Cheng: That alright so again over here, right. The link over here knows that the tech segment needs to be protected read only. 346 00:46:47.760 --> 00:46:58.500 William Cheng: Mode. So therefore, they need to be allocated in multiple pages over here and the data segment is, you know, the data side, but you need to be able to modify your global variable and also reach a global variable. So in this case, they need to 347 00:46:59.400 --> 00:47:09.360 William Cheng: Be protected read that right. Okay, so, so therefore we're going to keep them into different pages so that we can protect them are using different protection mechanism inside the Colonel. Yeah. 348 00:47:11.070 --> 00:47:22.980 William Cheng: All right, so, so that's how we're going to end up with this memory location right so data segment over here 163 a for this one is exactly 16 kilobyte right again a kilobytes 1024 so if you multiply 16 by 1024 you're going to get this number. 349 00:47:23.250 --> 00:47:28.290 William Cheng: Okay, so you will see that the tech segment over here, right. The textile going over here is so big over here, right. 350 00:47:28.800 --> 00:47:33.810 William Cheng: So as it turns out, three pages is going to be a little bigger. So here's page one, two and three over here. 351 00:47:33.990 --> 00:47:40.890 William Cheng: Okay, this will be a little bigger than the tech side, man. So the tech side when, at the end of the tech side over here, there, and there's actually going to be a little bit of waste of space. 352 00:47:41.310 --> 00:47:50.640 William Cheng: Okay, so in this case it's okay because we're going to make sure that on page four. That's what we have the data plus BSS like man, and they have to be protected using a different memory protection mechanism. 353 00:47:50.820 --> 00:48:04.920 William Cheng: To allow the data to be able to be to be read and to be written, you know, by your program. Okay, that they are the tech segment over here has to be read only. Okay, so this way you know your program will not be able to modify the program itself. That's okay. This is very important. 354 00:48:05.940 --> 00:48:17.070 William Cheng: All right, what about the rest of the address space. Okay, so you can see that you know the process has a 32 bit address space is the four gigabytes of memory. In order for us to run our program. How much memory do we actually need. 355 00:48:17.940 --> 00:48:27.180 William Cheng: Okay. So we see that, you know, over here, we need three pages right for the tech side, man. We need one page for the data segment over here. A plus BSS right. We don't need the heat. 356 00:48:27.570 --> 00:48:35.580 William Cheng: Memory over here because we don't call it, we wouldn't call Malcolm free. So therefore, we don't need heat, do we need a stock space. YEAH, WE NEED US taxpayer, so maybe that's why we need also one more page. 357 00:48:36.330 --> 00:48:48.450 William Cheng: Okay. So in this case, how much memory do any we actually, we need five pages. Each pages four to four kilobytes over here. So all we need is 20 kilobytes. Okay, so if we use up the entire four gigabytes of memory. That's a total waste. 358 00:48:50.310 --> 00:49:00.450 William Cheng: Alright, so, so, so again we are not allowed to allocate four gigabytes of physical memory just support our program because in the end, all the memory will be wasted because India, we might only need four kilobytes of data. 359 00:49:00.930 --> 00:49:04.230 William Cheng: So we only need 20 kilobytes of data. Right. So what is the solution now. 360 00:49:04.980 --> 00:49:11.100 William Cheng: So as it turns out. One of the comments solution in computer science one level in directions so indirection. You throw something about pointer. 361 00:49:11.310 --> 00:49:20.820 William Cheng: Pointer give you one level interaction. As I mentioned before, once you start using pointer, you can have a point to have a point to have a pointer have a pointer. So you can keep chasing pondering going to end up with many, many levels of 362 00:49:21.690 --> 00:49:34.530 William Cheng: indirection. Okay, so for the organism over here. It says, Well, you know, if you only want to allocate plenty kilobytes of address space. So plenty kill 20 kilobytes of memory. But in reality, your, your address basis for four gigabytes long 363 00:49:35.250 --> 00:49:39.300 William Cheng: Okay, so in that case the solution over here is that we're going to use one level interaction. 364 00:49:39.660 --> 00:49:46.110 William Cheng: Okay, the address coming out of CPU when I call them the virtual address and the address. We actually use inside physical memory. We're going to call that 365 00:49:46.770 --> 00:49:48.060 William Cheng: We're going to call that physical address 366 00:49:48.690 --> 00:49:57.570 William Cheng: Right, we're going to use the address translation mechanism to translate the virtual address to physical address. So in this case, will allocate only 20 kilobytes of physical address what the virtual 367 00:49:57.810 --> 00:50:04.710 William Cheng: Space that we use is going to be a four gigabytes of virtual address. Okay. So, therefore, most of our virtual address space is going to map to nothing. 368 00:50:06.060 --> 00:50:11.910 William Cheng: Right, because we only need 2020 kilobytes. In reality, so all of the other virtual addresses, they must add the mapping nothing 369 00:50:12.030 --> 00:50:21.450 William Cheng: So this way when we try to access. Nothing. We're going to get a segmentation fault and the opportunities and will kill off all that right, just like memory location zero, they're going to get mapped to nothing is going to be is going to get mapped 370 00:50:21.960 --> 00:50:28.890 William Cheng: To a memory location where there's no access. So, therefore, you can try to access them. You got to get a segmentation fall and the optimism mobile actually kill your program. 371 00:50:29.940 --> 00:50:39.300 William Cheng: Okay. So, using this particular technique of mapping virtual just to physical address in the air will only able to, we, we only need to allocate 20 kilobytes of memory to run our program. 372 00:50:39.690 --> 00:50:50.970 William Cheng: Our virtual address space is four gigabytes in size that right, so, so, so, so, so, so again, when we do when we do do this for this particular 373 00:50:51.630 --> 00:50:57.330 William Cheng: You know, a solution over here, we need some data structure inside the colonel to perform this address translation. Right. 374 00:50:57.870 --> 00:51:01.530 William Cheng: There. They're essentially that we use. He is our current oh this is no as the page table. 375 00:51:02.010 --> 00:51:10.290 William Cheng: Okay, it's the job of the page table to translate a virtual address to a physical address. Okay. So, pretty soon we're going to see an example what what that table look like that. 376 00:51:10.890 --> 00:51:16.170 William Cheng: Alright, so, so using you're using the page table to transition. Some of the virtual address over here to 377 00:51:16.830 --> 00:51:23.580 William Cheng: Translate into nothing. Some of the virtual address is going to get trying to translate into physical addresses that. So by using 378 00:51:24.000 --> 00:51:26.430 William Cheng: The sort of a page table couple for address translation. 379 00:51:26.850 --> 00:51:30.480 William Cheng: We're going to get the different page protection because every page can be protected differently. 380 00:51:30.660 --> 00:51:38.790 William Cheng: For the tech stack, man. They're going to be protected with the read only a protection mechanism for the data segment the PSS like man, we're going to protect them with rewrite access 381 00:51:39.120 --> 00:51:41.520 William Cheng: Okay, so this way you can modify global variables that 382 00:51:41.820 --> 00:51:51.030 William Cheng: We're going to talk about a lot of them in in chapter seven. To give you all the detail. So right now we're going to sort of give you some introduction, right. So, in chapter one already mentioned that we're going to use this idea of a virtual memory. 383 00:51:51.360 --> 00:52:01.530 William Cheng: virtual address the physical address and now you know in chapter three, we're going to sort of give you a little glimpse of a little more glimpse of what that looks like by showing you what a page table look like 384 00:52:02.460 --> 00:52:09.600 William Cheng: That alright so that's the next slide over here. Here's the page table on the left hand side over here I put them in grey. So this is just my own notes. 385 00:52:09.810 --> 00:52:15.450 William Cheng: So, so that you know so so I can sort of keep track of some of the stuff over here on the left hand side there on the right hand side over here. 386 00:52:15.720 --> 00:52:23.400 William Cheng: Is my actual table. Okay, so the patient will be here, you know, over here. So, what it will do is it will map a virtual page to a physical page. 387 00:52:23.670 --> 00:52:28.560 William Cheng: Right. So one of the virtual page over here in the tech Sacramento, we here, we mentioned before, there are mapping to three pages. 388 00:52:28.740 --> 00:52:39.960 William Cheng: And they are page number one, two, and three. Right. So again, page zero over here is for segmentation fall so page zero over here. What virtual addresses of correspond to right. So again, you're going to take this number of the page number 389 00:52:40.170 --> 00:52:52.110 William Cheng: Multiplied by four kilobytes was zero multiply four kilobyte still going to zero that so starting from from memory location zero inside your virtual address all the way to 4095 that will be the address is that page number zero 390 00:52:52.620 --> 00:53:03.510 William Cheng: Okay, so in that case what do we have access to it. Do we need to provide a page number zero, we need to provide no access to so this one. Will you try to access this memory location, you're going to get a segmentation fault and you're trapped inside a carnal. 391 00:53:03.870 --> 00:53:11.010 William Cheng: OK, so the way this is done is that inside the page table over here for page number zero, we're going to set a bit over here to say there's no access 392 00:53:11.940 --> 00:53:16.560 William Cheng: Okay, so this way where your program reference page number zero when we're going to perform the address translation to say 393 00:53:16.800 --> 00:53:26.040 William Cheng: Virtual case number zero which physical patient map to oh there's no access. So therefore, the Skype. We merely trying to the operating system kernel and then it'll kill you, that optimism current. Okay. 394 00:53:27.690 --> 00:53:34.170 William Cheng: All right, Richard. Please number 123 over here they are. They will be used to implement the tech side, man. Right. So the tech segment over here. 395 00:53:34.350 --> 00:53:41.760 William Cheng: Is going to go, it's gonna go three pages. The first memory locations. You guys can be 4096 and that will be the first memory location. Are we getting page, while 396 00:53:42.120 --> 00:53:50.160 William Cheng: Okay, so if you follow the pointer over here. So the first memory location over here starts at 4096 again. That's the base address when you prefer address translation. 397 00:53:50.580 --> 00:53:58.740 William Cheng: The access rights over here it's going to say that this page you can reframe it but you cannot write to it. And that's exactly what we want. Because we don't want your program to modify at the time segment. 398 00:53:59.010 --> 00:54:06.120 William Cheng: Now and then you follow the pointer over here that will point to the physical page over here. So the fiscal page over here will point to this physical page. 399 00:54:06.420 --> 00:54:13.950 William Cheng: Okay, so therefore 4000 and it says will get translated into physical address. That's the first memory location over here on this physical page. 400 00:54:14.880 --> 00:54:22.410 William Cheng: OK. So again, the way you should think about is that the main function over here are sitting over here inside the physical page. So when you start running your main function, you gotta start executing code right here. 401 00:54:23.400 --> 00:54:27.720 William Cheng: Now and then the cellular function is going to read the below it. Right. So the main function over here. 402 00:54:28.740 --> 00:54:29.970 William Cheng: So how many bytes. 403 00:54:31.530 --> 00:54:37.830 William Cheng: Bye bye bye to this. I don't if you remember it was 36 bytes. All right, so after 36 bytes over here. So again, it's a very tiny function. 404 00:54:38.040 --> 00:54:45.480 William Cheng: The next one is going to be so far right so bar over here is only a 24 by law right and then comes the printer function, the print function is sitting 405 00:54:45.870 --> 00:54:59.340 William Cheng: virtual address 4156 where I sort of gave you take four and five, six minus 4096 that will give you the offset in page one. So, again, in this case again when we perform address translation is going to point to the same physical page and you'll print it functions, right here. 406 00:55:00.480 --> 00:55:08.760 William Cheng: Okay, so the prayer punchy when you start calling Pranab is is going to start executing all the way to the end of this particular page over here. And when it goes to the area where does it go 407 00:55:09.330 --> 00:55:21.300 William Cheng: Well, so again this printer function. The function over here is in contiguous of your memory address inside the virtual address space. So this function started on page number one over here, it actually go all the way to page number three. 408 00:55:22.350 --> 00:55:30.120 William Cheng: Okay, so let me declutter this a little bit right printer function. So, right here. Okay, so in virtual address space actually go all the way to page three right here. 409 00:55:30.510 --> 00:55:39.930 William Cheng: And then sort of get in physical address space, page one over here to this page pays to over here if you follow the pointer will map to this page and paste three or 4.0 point right here. 410 00:55:40.740 --> 00:55:44.040 William Cheng: Okay, so if you look at if you look at the physical memory, whereas printer. 411 00:55:44.280 --> 00:55:52.290 William Cheng: Printer is going to start right here and you execute all the way to the end over here, y su go to the Eddie what jump over here to another page and it goes to execute and I'll be here. 412 00:55:52.470 --> 00:56:03.000 William Cheng: And when it finishes over here is going to jump to the next page, you know, jumping into play with a page like this that eventually you're going to reach the end the printer and the next function over, you will be the right function then followed by the startup option. 413 00:56:04.170 --> 00:56:15.210 William Cheng: Okay, so again, this is one of the power of one level indirection allows you to allocate memory or region in multiple pages and these pages. What are they, what are these pages come from these pages come from the buddy system. 414 00:56:15.990 --> 00:56:21.090 William Cheng: Because remember what you go through the buddy system. He said, Hey, give me a page. So what the buddy system over here will allocate all these pages for you. 415 00:56:21.360 --> 00:56:29.250 William Cheng: These pages can be scattered all over the place and physical memory, but in virtual memory space over here, they will be in contiguous virtual address face. 416 00:56:30.300 --> 00:56:39.420 William Cheng: Okay, so therefore in virtual memory over here. Their continuous memory location but physically. They're all over the place and this, this will give you a lot of flexibility of how to allocate physical memory. 417 00:56:40.410 --> 00:56:46.080 William Cheng: Okay, so a lot of times, whenever, whenever we use a one level one, level of indirection always give us a lot of flexibility. 418 00:56:46.650 --> 00:56:52.440 William Cheng: Yeah. Alright. So in this example over here, again, you know, the main function is right here. Suppose right here, a prenup sir over here. 419 00:56:52.620 --> 00:56:58.830 William Cheng: And then it spill over to this page over here and then a spill over to the other page over here and do we have the right and with the startup function. 420 00:56:59.370 --> 00:57:04.860 William Cheng: Well, what about the data segment over here, the data, somebody is on page four over here. So here's the data SPSS. 421 00:57:05.070 --> 00:57:13.710 William Cheng: Or the first memory location is an offset zero right here. Okay. You follow the pointer over here. A x is actually sitting at the first memory location inside this physical page. 422 00:57:14.100 --> 00:57:22.320 William Cheng: There. And as for bicycle. Alright, the next one will be as four by seven later is printed org and then all these variable be here psi. Second one over here, you know, pretty soon. 423 00:57:22.800 --> 00:57:36.540 William Cheng: Actually, they all sit inside the same page over here. So in the end, you know, we also wasted some space at the bottom of this physical page because our data segment over here is not really big and also we don't have. We don't have a heap segment, because we're not calling Malika free 424 00:57:37.590 --> 00:57:40.020 William Cheng: OK. So again, the entire dataset going over here is going to be 425 00:57:40.680 --> 00:57:50.550 William Cheng: Readable and rideable. So, therefore, if you try to modify one email memory location. So for example, if you say X equals to five over here. Why did you say you're going to go to this memory location. 426 00:57:50.760 --> 00:57:57.570 William Cheng: And since you're writing to a memory location is going to be compatible with the rewrite, you're going to go to physical memory over here and then you bought by this white, you said into five 427 00:57:58.650 --> 00:58:08.730 William Cheng: Okay, well, if you try to sell me equals five over here, what's going to happen is that, you know, the access protection over here will reject your operation and they are you going to tap into the operating system kernel and the current will kill your program. 428 00:58:09.720 --> 00:58:22.230 William Cheng: Yeah. So again, this is just a quick introduction to sort of show you how to use the page table together and we mentioned before he's not a colonel, there's this fancy data structure called the memory map right so the memory map and the page table together. 429 00:58:22.920 --> 00:58:32.730 William Cheng: Together they implement this address translation guy. So this way you have one level interaction, so that your address space so so so in this case, how many entries either inside this page table. 430 00:58:34.020 --> 00:58:46.080 William Cheng: Okay, so your address space is 32 bit risers to to the 3232 memory locations over here. Every page has four kilobytes. So that equals or two to the 12 if you divide these two number you're going to get to, to the 20th 431 00:58:46.350 --> 00:58:57.150 William Cheng: So this page table actually has 1 million entries in that. Okay, or two to the 20 over here is actually 1 million. So this is a really, really large table. Most of the entries over here are no access 432 00:58:57.630 --> 00:59:05.400 William Cheng: Okay. The only entries over here that have access are the ones actually mapped to physical pages. So again, the example that we gave over here. The only five pointers here. 433 00:59:05.610 --> 00:59:12.030 William Cheng: Three of them from the tech side when one of them from that data plus BSS and maybe one of them from the hoops that sort of stuff on the stack segment. 434 00:59:12.450 --> 00:59:15.660 William Cheng: Because when you whenever you run your sequel, you need you need 435 00:59:16.350 --> 00:59:26.370 William Cheng: You need a stag, in order for you to run your sea coast. Right. So again, another four kilobytes will be used over here, right, all the other entries over here the access entry over here, they will all have no access 436 00:59:27.000 --> 00:59:40.920 William Cheng: Okay, so this way. Again, this is how we can actually have a four gigabytes of virtual address and India, we only allocate 20 kilobytes a physical memory. Okay, so that's why you know in your, you know, inside your laptop, you'll be able to run 50 or 100 different application. 437 00:59:42.300 --> 00:59:54.780 William Cheng: Then, all right. So I'm going to end the lecture right here. So next time we're going to continue, you know, look into chapter three. Look at booty. And then we're going to start talking about chapter four and also get into chapter five. Yeah.