WEBVTT 1 00:00:01.949 --> 00:00:10.800 William Cheng: Welcome to the second part of lecture 30 so we only have one thing left to talk about that's chapter three, we're going to talk about Lincoln and loading. 2 00:00:11.700 --> 00:00:20.400 William Cheng: So we talked a little bit about linking already earlier on in chapter three, you know, we talked about static Lincoln and loading. We haven't really completely finished talking about it. 3 00:00:20.880 --> 00:00:36.150 William Cheng: So first want to sort of finish a static Lincoln and loading. And then we're going to, you know, take a look at dynamic link and then loading under the heading of shared libraries. Okay, so let's, let's, first you know finished static Lincoln and loading. 4 00:00:37.170 --> 00:00:37.980 William Cheng: Right, so 5 00:00:39.060 --> 00:00:48.960 William Cheng: So we see that a files, right, that if I was a library files. So the library file is just a collection of files. So you so you have a bunch of files over here. If I can kind of them concatenate them together. 6 00:00:49.380 --> 00:00:55.530 William Cheng: You're going to end up with a library file. Okay, and what we get. We're going to sort of see how to actually create a library file. 7 00:00:56.190 --> 00:01:09.240 William Cheng: In your Kronos I'm at right you see this live drivers, a little as five s.ai. So these are library files. So again, if you look at the content of that file using either the nm command. Right. The M commands mnemonics 8 00:01:09.750 --> 00:01:18.000 William Cheng: They actually show you a bunch of stuff about these files. And you can see that they actually divided into different sections. You can also run object dump on it, you will see also the content. Yeah. 9 00:01:18.900 --> 00:01:27.900 William Cheng: Alright, so there are two types of library. One is static library, which is the one with the you know what we're, what we're used to. And then we're going to sort of sort of talk a little bit about a dynamic link and then the 10 00:01:28.290 --> 00:01:35.040 William Cheng: Dynamic library. There are also known as shared library and Linux system. So I think in Windows a call it that every library. 11 00:01:35.490 --> 00:01:38.910 William Cheng: But they're basically the same thing. Right, we're going to talk about sort of tell you 12 00:01:39.480 --> 00:01:44.250 William Cheng: What they are without going into too much detail. Okay. Because a detailed over here is pretty complicated. 13 00:01:44.640 --> 00:01:50.760 William Cheng: Some people are doing that and you need the colonel three assignment by setting dynamic equals two, one. So once you're sending them equal to one. 14 00:01:51.030 --> 00:01:56.070 William Cheng: And you will see that if you run object dump on your executable file, you will see that it's completely empty. 15 00:01:56.670 --> 00:02:03.210 William Cheng: Okay, so there's only like one little function in them and everything else is the is not there, because everything else is loaded into memory and runtime. 16 00:02:03.780 --> 00:02:08.730 William Cheng: Okay so dynamic, meaning that things are done a runtime and static mean that things are done in compile time. 17 00:02:09.570 --> 00:02:17.550 William Cheng: Okay, so we have sort of a monolithic or no right. What happened is that you compile your code confined to one kernel and now you're kind of sitting on the desk and the the colonel represent 18 00:02:17.940 --> 00:02:21.990 William Cheng: The kernel has a complete representation of what it looks like when it's all running 19 00:02:22.860 --> 00:02:29.520 William Cheng: Okay, so therefore, at the time when you compile you determine the memory address of all your functions. Okay, so that static linking 20 00:02:30.240 --> 00:02:35.850 William Cheng: Okay, dynamic making is that a runtime. We're going to determine, you know, this function is that what address and the other functions of the different address 21 00:02:36.240 --> 00:02:40.860 William Cheng: Okay, so you can sort of see that it's gonna it's gonna be a very different, very different scheme. Okay. 22 00:02:41.640 --> 00:02:46.500 William Cheng: Alright, so let's take a look at how to create a static library, right. So we're going to use three silly example. 23 00:02:46.920 --> 00:02:56.880 William Cheng: One thought is cause someone Darcy inside. So what I see there's a function called sub one and then this function. All it does is a print the streets of one onto the screen. Okay, so instead of using print if 24 00:02:57.300 --> 00:03:02.610 William Cheng: You use a function called push string push Jane basically take the string and then write it to standard out 25 00:03:02.850 --> 00:03:10.470 William Cheng: Well guys, we can actually, you know, think about writing this one you're using system call right he's going to say right one, and then the string and the length of the string. And that's exactly what you would do. 26 00:03:11.040 --> 00:03:22.620 William Cheng: Okay, so again this is a very, very simple see library function. Okay, the second file here costs up to Darcy inside there. There's a function costs up to and all of us to Prince up to standard out 27 00:03:23.280 --> 00:03:28.770 William Cheng: The third files course of three Darcy. There's a bunch of yourself three and it print, you know, sub three to standard out 28 00:03:29.070 --> 00:03:35.820 William Cheng: There. So when we say GCC minus c we use minus see that, see if I'll get compounded that oh file right so we use that Jesus minus c 29 00:03:36.000 --> 00:03:42.360 William Cheng: And you can list all your CFO and they will all get compared to that file. So after this. If you say LS, you're going to see the sci fi and also 30 00:03:43.230 --> 00:03:47.130 William Cheng: Okay. And then what you do is that you combine all the data files together to create a library. 31 00:03:47.430 --> 00:03:55.320 William Cheng: There. So the command that you use something called AR AR which means the archive command. So the article that said, I want to create a file and then here's and then what 32 00:03:55.830 --> 00:04:08.010 William Cheng: Is the name of the profile that so in Unix and Linux, the naming convention for a static library is you start with a word Li be and then followed by the name of the library. And then in the end you say 33 00:04:09.870 --> 00:04:15.750 William Cheng: Okay, so in this case we have a live print one day, right. So this is the private library. Number one, or whatever you call it. 34 00:04:16.020 --> 00:04:21.270 William Cheng: Okay, so this is private number or the library. Number one, and then live in a, these are you know so it's all about convention. 35 00:04:21.570 --> 00:04:33.570 William Cheng: Okay, so in this case what we do do is that we take all these profile was simply concatenate them together, one after another, right. So, this particular file over here. This is what it will look like. It has three sub section in that one is the one 36 00:04:33.990 --> 00:04:36.480 William Cheng: That always up to that. Oh, and dry stuff out. 37 00:04:37.920 --> 00:04:42.450 William Cheng: Okay, so that's what a library is the library simply account, candy, candy concatenation of budget file. 38 00:04:42.930 --> 00:04:45.480 William Cheng: Now also, by the way, you have seen the command already 39 00:04:45.780 --> 00:04:56.430 William Cheng: The command that you use is called car race car. So Tara is a tape archive file. So in this case, you've been to create a bunch of you're going to concatenate a bunch of files together and then you write it to the tape. 40 00:04:56.730 --> 00:04:59.820 William Cheng: I mean, what does it take you want me never seen a tape, you know, these days. 41 00:05:00.210 --> 00:05:06.150 William Cheng: So in the good old days, there's a device called tape and you can actually, you know, have you sort of, you know, archive, all these things together and writing 42 00:05:06.600 --> 00:05:11.100 William Cheng: And then combine them into one file and you write this file are, you know, ready to the tape. 43 00:05:11.670 --> 00:05:22.740 William Cheng: Okay, so that was the tape at the top, the document. Right. So to create a tar file. What do we do is that we are right. See the app or something like that. You can see the see right here, the user are over here. 44 00:05:23.550 --> 00:05:29.130 William Cheng: I don't know what the R stands for, maybe it's something related a library or something like that. Right. It doesn't really matter. 45 00:05:29.940 --> 00:05:34.200 William Cheng: Oh, I think our puppies mean that it's real locatable because we talked about these 46 00:05:34.530 --> 00:05:45.180 William Cheng: Are they all relatable. So maybe ours relatable. Right. So anyways, so when we finished doing this, you're going to end up with a live proven that A and this one is a bunch of concatenation of Atari profile. 47 00:05:45.630 --> 00:05:51.390 William Cheng: So later on when you try to link with this library. How do you link with the library. So to get the link or what it needs to do is that needs to do. 48 00:05:52.170 --> 00:05:57.540 William Cheng: Link has two functions. One is to to do relocation and the other one is called simple resolution. 49 00:05:57.930 --> 00:06:04.140 William Cheng: Whereas I want to propose symbol resolution what it needs to do is need to find a function, right. So, what it will do is that it will go through this fall over here. 50 00:06:04.500 --> 00:06:11.940 William Cheng: Open up every one of these files scan through it to see where the function is. If it's not in the first function over here you will go to the second one, it will go to the third one. 51 00:06:12.630 --> 00:06:18.480 William Cheng: Okay. I mean, clearly, if you have a large library like the C library doing this every time for every little function. It will take you too long. 52 00:06:19.020 --> 00:06:28.710 William Cheng: Okay. So typically what people would do is that they will run another command called the archive command and this one, you say tea means to generate a table of contents, you know, for this particular file. 53 00:06:29.220 --> 00:06:36.000 William Cheng: Okay. So in this case, what you would do is that you add a table of contents over here on top of it. So this way when you try to perform a function, right, so what's the table. Come on. 54 00:06:36.390 --> 00:06:42.240 William Cheng: Okay, what content is kind of some kind of a map data structure, it will map a string to the location inside this file. 55 00:06:42.600 --> 00:06:52.440 William Cheng: Okay, so this way when you try to perform, you know, linking you can easily find the definition of a function, you can copy all that function that the test tech segment that data segment and the segment copied me to 56 00:06:53.670 --> 00:06:55.170 William Cheng: copy them into executable program. 57 00:06:55.950 --> 00:07:01.680 William Cheng: Okay. So typically, if you want to speed things up, you will have to run the other the table content commit right 58 00:07:01.800 --> 00:07:10.470 William Cheng: You can also combine these two things together, you can say are followed by TC are over here. And then what it will do is it will create a library file and also create a table of content for it. 59 00:07:10.830 --> 00:07:24.030 William Cheng: Yeah. Alright. So we run this command, what it will do that. It will show you the data files processing. So the processing through the sub windows 2003 though, and then the process. He will be done and now your library file will have a have a table content. Yeah. 60 00:07:24.840 --> 00:07:30.150 William Cheng: Alright, so there's a comment over here in the end over here to say that push string is unresolved and live pro 61 00:07:30.780 --> 00:07:40.500 William Cheng: Wrestling via your users you create this file, this file is still called Live provide that a there. So if you try to sort of figure out, you know, where, you know, what is the memory location for pushing 62 00:07:41.400 --> 00:07:51.270 William Cheng: Okay, we saw that before I started thinking, you know, here we will India where he generated executable file in the executable file can actually see that main is going to be this address. And then, you know, 63 00:07:52.410 --> 00:07:56.550 William Cheng: I can't remember what are the functions that were sub we're going to be this address and print have it's going to be this address 64 00:07:57.030 --> 00:08:01.290 William Cheng: So we actually figure out, we will have figured out all the addresses, right, because that's the job of the linker 65 00:08:01.710 --> 00:08:07.560 William Cheng: Okay. So, so far we haven't run any of the linker is. Yeah. So therefore, we don't know the actual address all these function. 66 00:08:08.250 --> 00:08:15.420 William Cheng: Okay, it's almost like in the beginning when we try to generate all the data file. We don't know their final address because we're waiting for the linker to to to to 67 00:08:15.840 --> 00:08:23.730 William Cheng: Perform relocation. Okay. So, similarly this program today. We just concatenate a bunch of us together. We don't really know what virtual addresses. 68 00:08:24.210 --> 00:08:31.170 William Cheng: They got to take on. Right. So, therefore, a function like pushing over here. They were unresolved, you know, inside, inside the library file. 69 00:08:31.860 --> 00:08:37.920 William Cheng: Okay, so typically means that a library file, you know, the you know the the the the addresses are unresolved. OK. 70 00:08:38.700 --> 00:08:43.230 William Cheng: But so, so they're waiting for the link or to eventually figure out what the address are right, we saw before, right. 71 00:08:43.710 --> 00:08:54.030 William Cheng: When eventually when you need to print out you need to go to the C library fine print a half and then in that, in that case, when you do that, you're going to assign printer. A final, final virtual address. Okay, so up to them. 72 00:08:55.620 --> 00:08:57.660 William Cheng: That they are unresolved. Okay. 73 00:08:59.280 --> 00:09:03.270 William Cheng: Alright, so let's write a program to use it right here is a my program my program is may 74 00:09:03.450 --> 00:09:13.650 William Cheng: It's inside practice. See, and I say, step one, step two. And step three. And now when I try to compile this call. I need to say I need to link with a proven library because otherwise I'm going to, I'm going to get a good gig. 75 00:09:13.950 --> 00:09:23.100 William Cheng: I'm going to get a link or error. Yeah. So therefore, the way I do this as I as I GCC minus oh follow minus is the executable program name right. So mine is over here is going to be Prague. 76 00:09:23.340 --> 00:09:30.750 William Cheng: And then here's the source file Prague. I see over here and then I will tell the compiler or I will tell the linker where to find my library. 77 00:09:31.230 --> 00:09:42.630 William Cheng: Okay, minus capital L to say I need to find like the library in the current working directory. Okay, that is a current working directory and then the name the library. I need to look for to find these function is called the prevent library. 78 00:09:43.170 --> 00:09:54.150 William Cheng: OK. So again, what the you know what the lingo will do is it will add live in front of it, at that. A after it and try to find this file. If it tried it found this thought in the current working directory, then it will perform, you know, 79 00:09:54.510 --> 00:10:01.830 William Cheng: symbol resolution and then again copy all those code into the executable. Right. Alright. So in this case, you know where this question comes up. 80 00:10:02.520 --> 00:10:08.700 William Cheng: Okay, so we're here, right. You can try to look for pushing, pushing, is not in progress that you pushing is not English pre one day. 81 00:10:09.330 --> 00:10:17.760 William Cheng: So as it turns out, what happened is that when you type a command like this you know the the GCC compiler, they will actually add these invisible argument at yet. 82 00:10:18.180 --> 00:10:26.730 William Cheng: OK, so the invisible girl would tell GCC were to actually find or actually actually tell the linker. The LD linker. You know where to find additional libraries. 83 00:10:27.090 --> 00:10:31.620 William Cheng: OK. So the typical library is going to be the C library. So over here, there was a go to minus 84 00:10:31.980 --> 00:10:44.280 William Cheng: Elegant specify directory. Go to slash slip. What you should look for the C library and the library is live. See that a okay so inside slash live over here, there should be a focus lips, lips, see that a 85 00:10:44.760 --> 00:10:51.510 William Cheng: So this way, they will be able to find the pushing function. Okay. Otherwise going to get an error to say symbol now found or, you know, 86 00:10:52.710 --> 00:11:00.600 William Cheng: Yeah, so, so I guess that's one of the messages that you see when you are, you know, writing your code for warm up wanting to yeah so so again you have to write this correctly. 87 00:11:01.170 --> 00:11:03.540 William Cheng: You know, in order for it to find to find the library. 88 00:11:04.320 --> 00:11:12.840 William Cheng: There's a note over here to set the order of the libraries matter guys over here, you know, we have privilege on the left hand side and see the C library on the right hand side. 89 00:11:13.020 --> 00:11:18.030 William Cheng: So this way when we try to locate a library. What we need to do is that we need to look from the left to the right. 90 00:11:19.020 --> 00:11:25.380 William Cheng: OK. So the rule in the compiler and the linker is that the first time you find a function that will be the one that will be the one that you use. 91 00:11:25.740 --> 00:11:34.320 William Cheng: That. So in this case, you know the the product. I see over here it's using sub one. Step two. And step three. So step one, step two. And step three, there are found in the library. 92 00:11:34.770 --> 00:11:43.320 William Cheng: So inside of one sub sub three, they call the string function. Right. So where's the push function or the pushing function in the C library. So, therefore, you know, all these library in the right order. 93 00:11:43.890 --> 00:11:47.820 William Cheng: Okay, if you make them in the wrong order. Sometimes the compiler will not be able to find it. 94 00:11:48.660 --> 00:11:54.600 William Cheng: OK. So I guess the modern compiler GCC sort of become very, very smart when they cannot find out that we actually try things in different order. 95 00:11:55.020 --> 00:11:58.980 William Cheng: Being the older version of GCC. If you listen in the wrong order while you're gonna get into trouble. 96 00:11:59.370 --> 00:12:07.080 William Cheng: Okay, I don't know if you if you actually run into trouble, your warm up one and two. Sometimes when you put in the wrong order that the compiler, the linker sort of refused to 97 00:12:07.590 --> 00:12:09.060 William Cheng: Finish compiling that 98 00:12:09.840 --> 00:12:10.140 William Cheng: Alright. 99 00:12:10.560 --> 00:12:22.170 William Cheng: So again, this is one of the rules that are used by GCC, we should look for a symbol you go from left to right. Right. So this way, in case you have to library have exactly the same symbol what in that case this will make a difference. Right. 100 00:12:22.350 --> 00:12:27.270 William Cheng: You know, because if you look for that in a in a different order. You're going to pick up a different implementation of the same function. 101 00:12:28.050 --> 00:12:36.990 William Cheng: Okay, so when a function appear in multiple libraries. So the first time you will see it. That will be the first time the lingo was here that will be the one that will be used by the linker 102 00:12:38.160 --> 00:12:46.560 William Cheng: Alright, so after you perform this operation over here that you run the program right now slash Prague and you will see that step one, step two. And step three, they're going to print step one, step two and. Step three. 103 00:12:46.950 --> 00:12:52.740 William Cheng: And also, you know, there is no backslash and in the end. So the print out. It's kind of agree. OK, so maybe that's not what you want. 104 00:12:53.370 --> 00:12:55.200 William Cheng: Okay, so if that's not what you want. What do you do 105 00:12:55.410 --> 00:13:06.510 William Cheng: Right, so you really cannot change the behavior of the C library. What you can do is that, you know, since I just described this rule you can stick a library in front of it. And if you can use your own implementation of pushing you can actually 106 00:13:06.720 --> 00:13:14.880 William Cheng: You can actually take over the behavior of the C library. Okay, so this way you know if you have any kind of functions that to see like where you don't like you can easily modify them. 107 00:13:15.480 --> 00:13:27.000 William Cheng: Okay, so, so, so this. The other can be done. So we're going to do that we're going to write our own version of the pusher in function right will be here. Here's my pushing Darcy over here, right. So, so in this case I need to implement my own pushing function. 108 00:13:27.600 --> 00:13:32.640 William Cheng: So this guy is what you're supposed to do again tried to print the string out. So in this case, what I would do is I 109 00:13:33.000 --> 00:13:40.290 William Cheng: Again, I'm supposed to write your standard out right so we here. I'm going to call the racism call and then I'm going to. So first print my posts string over here. 110 00:13:40.530 --> 00:13:46.530 William Cheng: And this one is nine characters long right 123456789 without the zero at the end. 111 00:13:46.800 --> 00:13:56.070 William Cheng: So I will first print nine characters and then followed by the string and then a number of characters. I will print will be the string length of the string. And then finally, I will print a backslash 112 00:13:56.880 --> 00:14:04.260 William Cheng: Okay, so this way every time. When you call posturing. Okay, I'm going to end up printing a line that has this one at the beginning, over here, followed by the street and if I like 113 00:14:04.680 --> 00:14:12.030 William Cheng: backslash and so therefore, the output is not going to look ugly as before. Okay. But in this case I'm overriding the the behavior here. 114 00:14:12.480 --> 00:14:18.480 William Cheng: So in this case I will return the value I don't really know why. In return, the one guess I'm not familiar with pushing. I never use it. 115 00:14:18.900 --> 00:14:23.550 William Cheng: So you need to look at the man page and see what pushing supposed to do, and maybe you should return the same thing. Yeah. 116 00:14:24.360 --> 00:14:32.640 William Cheng: Alright, so what I will do is I'll compile on my screen guys seen to my pushing out. Oh, using GCC minus C and then I will generate a library right so 117 00:14:33.150 --> 00:14:43.470 William Cheng: This one is called my stream library right live followed by my posturing, followed by eight. So this one is my pushing library. And then in this case I will use only using the Preview, File. 118 00:14:43.770 --> 00:14:52.080 William Cheng: Mean this case, I can also generate a table content, but it will be kind of silly because because you know this library only has one function and so therefore you should be really easy to find. 119 00:14:52.590 --> 00:14:57.810 William Cheng: So therefore, I don't really need a table of content. Okay. Alright. So then what I would do is I try to combine my program again. 120 00:14:58.020 --> 00:15:05.460 William Cheng: Now when I tried to perform linking I first I will link to my version of the pushing function before the C library version of the position function. 121 00:15:05.880 --> 00:15:12.360 William Cheng: Okay, so therefore, in this case, I was still says minus capital L, followed by doctor said, I want to look for library in the current working directory 122 00:15:12.570 --> 00:15:18.150 William Cheng: And then I will first look at the private library. Number one, and then I will look into my pushing Darcy. 123 00:15:18.630 --> 00:15:22.800 William Cheng: And so again if you reverse the order over here, then you're going to pick up the wrong one, right, because the 124 00:15:23.160 --> 00:15:30.540 William Cheng: The private library. Number one, that's the one that called posturing. If you reverse the order. What then you will try to look for pushing function after private library number one. 125 00:15:31.050 --> 00:15:42.630 William Cheng: Okay, but if you do it this way. Right. The C library, the you know the the the the see libraries at the end over here. So in this case, when I tried to perform symbol resolution when it cannot. It will want to try to locate pushing 126 00:15:42.870 --> 00:15:46.980 William Cheng: What it will do is that I will look to the right and then we'll pick up your implementation that pushing function. 127 00:15:47.670 --> 00:15:52.620 William Cheng: And that's by the way you're pushing a bunch of over here. Why doesn't he just called the pusher and function in the C library here. 128 00:15:53.460 --> 00:16:01.860 William Cheng: Well, they want because because that will confuse the compiler, right, because if this function is called clustering. If you call pushing again inside the compiler is going to think that you're making a recursive function call. 129 00:16:02.220 --> 00:16:12.510 William Cheng: I mean clearly that's not you. That's not your intention, so therefore inside this function over here. We're going to remit RE. RE implement pushing and also we need to put additional stuff around it. Okay. 130 00:16:13.290 --> 00:16:22.590 William Cheng: Alright, so once you do this. So, so when you run a das ash prod right now you can see that good behavior is change. We haven't really to modify the C library. So now what it will do is that it will 131 00:16:22.890 --> 00:16:31.440 William Cheng: Print out my purse strings, you know, followed by a by a space and then subway and they followed by by a backslash and so if this is the way that you want things to look where you can even read your code this way. 132 00:16:32.850 --> 00:16:34.380 William Cheng: Okay, so, so this is, you know, pretty 133 00:16:34.620 --> 00:16:41.550 William Cheng: Convenient. Right, especially, you know, in the case where you know the the you know the the private library over here. If it's written by somebody else. 134 00:16:41.700 --> 00:16:53.310 William Cheng: And the C library is also written by somebody else why, in this case, you can actually write your code. This way, and then you know the previous library now is going to end up with a different behavior. Okay, so this is actually pretty convenient if you can use this trick that 135 00:16:54.390 --> 00:17:03.570 William Cheng: Are so there's no substitution right with substitute the behavior of the original pushing function inside the library with all that. Okay, this is a neat trick. 136 00:17:06.930 --> 00:17:16.410 William Cheng: Alright, so, so, so now we are finished with the static library. The next thing we're going to see is, you know, is dynamic library. So, so what is wrong with static library. 137 00:17:17.040 --> 00:17:24.630 William Cheng: Okay, static library over here, you know, this is a weapon is that if you think about your warm up to our warm up. Why, right, one of the function that you call a lot 138 00:17:25.830 --> 00:17:26.310 William Cheng: Is printed 139 00:17:27.390 --> 00:17:36.030 William Cheng: Okay so so often is that you know so lesson that we're running on a shared server. I guess there's something called B2B SCF one of the trivia see up to the usc.edu 140 00:17:36.270 --> 00:17:40.200 William Cheng: So those are the sheer server if we're doing our programming the summit on that machine. 141 00:17:40.530 --> 00:17:51.840 William Cheng: Okay, so see us world to we have over 240 students. So in this case, every piece of warm up one every piece of warm up to. They all have a copy of the printer function inside. Inside executable file. 142 00:17:52.590 --> 00:18:02.940 William Cheng: Okay, we saw before that the printer function is pretty big. Right. And the example that we saw is 12,000 bytes law. So in this case 12,000 buyers going to multiply by 240 times and why it's clearly a waste of this space. 143 00:18:03.270 --> 00:18:10.680 William Cheng: Okay. And also if everybody's running one month one a warm up to. We have 240 processes that are running. Everybody get a copy of the same function. 144 00:18:11.010 --> 00:18:18.720 William Cheng: I mean the printer function over here. They are exactly the same code, right. So clearly, they can be shared. Okay. But since we're using static library what everybody will get a copy 145 00:18:19.530 --> 00:18:22.410 William Cheng: Okay, so when everybody's running this program. You're also wasting memory. 146 00:18:23.040 --> 00:18:27.330 William Cheng: Okay. So the problem with the study libraries that you're wasting this space. And you're also wasting memory. 147 00:18:27.570 --> 00:18:34.500 William Cheng: Right. So what do we really want. What we want is that on the desk there should be only one copier printer and not 140 copies of entrepreneur. 148 00:18:34.800 --> 00:18:44.760 William Cheng: Also, when you run your a C library right print up there should only be one copy because it's read only. So, therefore, you can share or you want. Okay. So in our case, we should only load one copier printer. 149 00:18:46.200 --> 00:18:58.560 William Cheng: Only one copier printer into into memory and this way all your all our program can actually share the same function there. So that will be the sort of the ideal case right so we're gonna see how to actually achieve this ideal case that 150 00:18:59.670 --> 00:19:12.210 William Cheng: All right, so, so we sort of need a way to share printer right here is, you know, the C library print print. So maybe you know in the C library over here. We're not going to copy to processes and also where you run it. We only know one copy into into memory. Yeah. 151 00:19:13.200 --> 00:19:22.890 William Cheng: All right. So here there's a comment is that if printer requires no relocation, then it will be easy. Right. So what we can do is that, you know, you know, so, so maybe we can actually 152 00:19:23.640 --> 00:19:30.180 William Cheng: Have a linker that will actually pre allocate enough to be at the same addresses for everyone. Okay. So in our case, you know, every 153 00:19:30.810 --> 00:19:38.190 William Cheng: One of these process when they try to call printer, they all use exactly the same virtual address or in this way, we only have we only want to recall one copy 154 00:19:38.550 --> 00:19:48.690 William Cheng: Because of C library over here might be able to do something like that low one copy to the library and then we can set up everybody's memory map so that they will point to a printer using exactly the same virtual address. Okay. 155 00:19:49.170 --> 00:19:54.960 William Cheng: But in reality, it's really, it doesn't really work that way. Some people's program is smaller. Some people guys larger 156 00:19:55.140 --> 00:20:02.190 William Cheng: They all want to use the print up, I'm sure. So the printer, you know, a function over here. It's very difficult to sort of figure out one address for every process. 157 00:20:03.180 --> 00:20:09.150 William Cheng: Okay, so in the end printer is going to end up with a different virtual address inside different processes. So in that case. Well, what can we do 158 00:20:10.020 --> 00:20:18.960 William Cheng: That. So again, we can that we can still continue to use idea of memory napping right so let's guys you know what you can do is that you can try to load the printer function into memory once 159 00:20:19.350 --> 00:20:26.850 William Cheng: Okay, and then you can make this memories Admin, Shared by all your user processes based by setting up the virtual memory map right so this way you know this product. 160 00:20:27.180 --> 00:20:32.880 William Cheng: Process. A over here will call printer in one address and process me over here will call printed in a different address 161 00:20:33.720 --> 00:20:38.280 William Cheng: OK. So again, using one level in direction. That will give you a lot of flexibility so maybe this can be done. 162 00:20:38.730 --> 00:20:41.820 William Cheng: But, but as it turns out, this wasn't really this is really not enough. 163 00:20:42.270 --> 00:20:50.610 William Cheng: There because in reality, over here, this function, you know the function that you want to call it may be very, very complicated and this function. My call another function inside C library. 164 00:20:51.480 --> 00:20:57.300 William Cheng: Okay, so in that case is going to get a little weird guy so so let's take a look at that situation over here now. So let's say that you know 165 00:20:58.260 --> 00:21:07.170 William Cheng: We're going to load the shared library into physical address 1200 over here, there, and then in process. A over here. They want to call print have a virtual address 10,000 166 00:21:07.380 --> 00:21:12.000 William Cheng: In process me over here. They want to call you know the function of virtual address 20,000 167 00:21:12.360 --> 00:21:20.460 William Cheng: Okay, so you can actually see that using virtual memory map using the page table. I can map this virtual address to to to to the same physical address but both of these processes. 168 00:21:21.090 --> 00:21:28.110 William Cheng: So again, doesn't have you actually share you share the same page and you will point to the same physical page. And now, both of them. They can use different virtual address to call the same function. 169 00:21:28.680 --> 00:21:37.200 William Cheng: Okay. So, so far so good, but what is this function over here, try to call another function inside of C library. Okay, so let's say that instead of C library. There's another function called do print 170 00:21:38.100 --> 00:21:41.370 William Cheng: Yeah so print have is going to call another function, go do print it, or look like this. 171 00:21:41.910 --> 00:21:52.530 William Cheng: Guy's and this guy is printed book called do print and do equipment sitting over here. So let's say that the physical address over here is 2200. Okay. So in this case, why should we compile this function called into 172 00:21:54.330 --> 00:21:56.880 William Cheng: Okay, so, because what happened is that where you say that 173 00:21:57.570 --> 00:22:14.520 William Cheng: This address just happened to be inside the same page as over here. This is going to be 1000 memory location later. Okay. So, therefore, if you're in process. A you want this particular function you want you want this particular function call get compiled into call into call 11000 174 00:22:15.750 --> 00:22:29.580 William Cheng: Okay, so what process. A you want this one to be compliant to machine instruction call 11000 right. But what if your process be your if your process be what you want to do is that you want to call this function at, you know, so you want to compare the sounds you into call to 1000 175 00:22:31.170 --> 00:22:42.870 William Cheng: Okay. So in this case, you know, if you look at the sheer C library over here, right. So, so this one needs to be pre compiled. If you want to freak out when you come into right you can compile into this instruction, you can come into that instruction. So it's not gonna work. 176 00:22:44.190 --> 00:22:52.560 William Cheng: Alright, so even though we can share the entry point into the shared library over here. But you know when we start making other function calls at the shared library while then then this particular schemes are going to work. 177 00:22:53.790 --> 00:22:58.380 William Cheng: Okay, so therefore I get what is going to be the solution. The solution is going to be another level of indirection right because 178 00:22:58.980 --> 00:23:03.510 William Cheng: That's a solution that seems to work for everything. So why not here. Yeah. So let's take a look at it, you know, that's 179 00:23:04.050 --> 00:23:09.840 William Cheng: What sort of talk about, you know, three different kind of solution. And one of them is going to use one level indirection. 180 00:23:10.620 --> 00:23:17.010 William Cheng: Okay, so, so the basic approach over here, right. Number one is limited sharing over here just said, well, why don't just give up. 181 00:23:17.280 --> 00:23:22.290 William Cheng: Okay, so we allow them to share on the desk. But then when you start running your program. Everybody get a different copy of it. 182 00:23:22.590 --> 00:23:35.910 William Cheng: Okay so clearly that will work. Right. So he said in every address space. We have a different copy or print up again we're wasting memory. So this solution is not going to be a set acceptable to us. So even though it is a solution, but it's really not a good solution now. 183 00:23:36.420 --> 00:23:41.670 William Cheng: Number two is the one that I described before. For every function you predetermine what address, you're going to use 184 00:23:42.030 --> 00:23:45.990 William Cheng: Okay, so this way, every function, right, including printer, including you wanted to do print 185 00:23:46.230 --> 00:23:53.130 William Cheng: They all you know use a fixed virtual address right so this way you know on process and process, be they will call call it exactly the same. 186 00:23:53.430 --> 00:24:03.240 William Cheng: They will use exactly the same virtual address but in that case this code over here will also be able to, you know, use it. Use his own virtual address and now they all agree on a virtual address, so therefore it's going to work. 187 00:24:03.990 --> 00:24:10.980 William Cheng: OK. So again, this is really not very flexible. If you end up you have small program you large program and you want to move things around why that's not really very flexible. 188 00:24:11.310 --> 00:24:17.880 William Cheng: Okay, because if you do it this way, every function inside the shared library over here, you have to preach predetermine what their virtual addresses are 189 00:24:18.240 --> 00:24:28.260 William Cheng: Guys are pretty much nobody really do something like that what people will do is that they are in the end they will they will take the third approach to use one level interaction. And that's why as know as position independent 190 00:24:29.130 --> 00:24:35.580 William Cheng: Okay, so if you use positioning dependent call there's really no need for relocation as we turn. As it turns out, relocation doesn't really work very well. 191 00:24:36.000 --> 00:24:41.250 William Cheng: Okay, so in this case we're going to sort of produce code that can be placed anywhere in memory, and they all going to work. 192 00:24:41.490 --> 00:24:48.420 William Cheng: It doesn't really matter what virtual address you use, right. So this is called position independent code, you can put this function anywhere you want. And it's going to work. 193 00:24:49.410 --> 00:24:54.900 William Cheng: OK. So, again, in order for us to do that, we need to use one level and direction. Yeah. Alright, so, so in this case. 194 00:24:55.560 --> 00:25:02.010 William Cheng: What does that mean one love One Direction. Right. So instead of making a direct function call and now you're gonna you're gonna make an indirect function call. 195 00:25:02.370 --> 00:25:07.890 William Cheng: God, what's an indirect function call right in the reference to go look like this is a call, followed by a register name. 196 00:25:08.550 --> 00:25:13.800 William Cheng: Okay, so what happens inside registered over here. We're going to store the agile, you're going to store the virtual address of a function 197 00:25:13.950 --> 00:25:22.800 William Cheng: So we use a call, followed by the name of the register. We're going to take that register and then we're going to look at the content of there and instead of content. There's a virtual address and that will be the address that we're going to use 198 00:25:23.700 --> 00:25:28.350 William Cheng: Alright, so again, this is one level interaction, as it turns out it works really well you know for something like this. 199 00:25:29.070 --> 00:25:34.500 William Cheng: Yeah. So let's take a look at the sort of the, the, the implementation over here. 200 00:25:34.890 --> 00:25:42.540 William Cheng: The basic idea here is that, you know, if I'm process. A. Okay, I'm going to build a table that contains all the virtual address for all the function inside the shared library. 201 00:25:42.930 --> 00:25:51.210 William Cheng: Okay. So in this example, all the functions that I should I be there to function right what it's called printer. The other ones called do print. So I'm going to build a table over here. Okay. 202 00:25:51.600 --> 00:25:57.300 William Cheng: This was a lookup table for every function is that a shared library. I'm going to say what the virtual address that I would like to use. 203 00:25:57.570 --> 00:26:06.660 William Cheng: Okay, so this case I want printer have to be located at virtual address 10,000. I also want to print to be located at 1000 instruction later to be 11,000 204 00:26:07.290 --> 00:26:16.230 William Cheng: OK for process be process, we want to call this function at a different address. So over here I have a table right it will be here this as you know Pranab is going to be located a virtual address 20,000 205 00:26:16.440 --> 00:26:29.850 William Cheng: And then do print is going to be located at different address over here compared to process a little bit 21,000. Okay. So in this case, both process. A and B is going to be able to have all these addresses at the address, they want. Okay, so this case, we're not doing pre pre 206 00:26:30.990 --> 00:26:34.740 William Cheng: The pre relocation. Yeah. But in this case. 207 00:26:35.400 --> 00:26:40.080 William Cheng: The way we're going to call the function is that we're going to use a register indirect function call guide to call the assumption 208 00:26:40.350 --> 00:26:43.950 William Cheng: That. So we're going to do is, I will process it over here if you want to call printer. 209 00:26:44.220 --> 00:26:49.230 William Cheng: What you would do is that, you know, so, so this table that we see right here, there will be pointed to buy a register. 210 00:26:49.440 --> 00:26:56.520 William Cheng: Okay, so let's call this register our wine. So I was going to point to the you know the register. So again, you can think about this one is a data structure over here. 211 00:26:57.000 --> 00:27:01.230 William Cheng: That you have a function name followed by a virtual dress. So it's kind of like a rate data structure. 212 00:27:01.530 --> 00:27:07.140 William Cheng: That array index zero is going to give you 10,000 or a index one over here is going to give you 11,000 213 00:27:07.380 --> 00:27:15.990 William Cheng: Well on process be over here again you pointer to with exactly the same register over here. So array index zero will give you 20,000 or a index one over here. Give me 21,000 214 00:27:16.950 --> 00:27:21.030 William Cheng: Okay, so what we're going to do is that when we all will will will will ready to call printer function. 215 00:27:21.210 --> 00:27:29.280 William Cheng: What we were first we're going to use our one has a base register use printer function as an array index. And then we're going to load this value over here into a different register cartoon. 216 00:27:30.090 --> 00:27:37.530 William Cheng: Okay, we're going to serve. So there's a CPU ratio of your car to right. So when we are process. A over here. The first thing that will do is I'm going to load our to 217 00:27:37.650 --> 00:27:46.020 William Cheng: With our one as a base address with print as a printer as the array index. So this one we're going to get 10,000 so our to over here we have the value 10,000 218 00:27:46.650 --> 00:27:50.700 William Cheng: Okay. And the next thing that we do that we're going to make a register indirect call we're gonna say call our two 219 00:27:51.570 --> 00:27:57.450 William Cheng: Okay, so what we call our to which ones you're going to end up calling again I'm calling a printer at virtual address 10,000 220 00:27:57.840 --> 00:28:03.270 William Cheng: Okay, so therefore, in this case, what are you calling this function over here and this function will be located virtual address 10,000 221 00:28:03.930 --> 00:28:11.070 William Cheng: OK, so now inside printer have what we call the to print function we're going to also use pre position in America. So pretty much, once we start using 222 00:28:11.280 --> 00:28:15.870 William Cheng: A position independent call all the calls over here. They all have to use the same calling convention otherwise. 223 00:28:16.350 --> 00:28:23.070 William Cheng: Otherwise, I'm going to work. Okay. So inside the shared library. Well, you need to make a function call. You also have to use position independent code. 224 00:28:23.850 --> 00:28:34.920 William Cheng: That. So in this case over here. If I want to call do print I use exactly the same code as this why except that you know the array index will be different. Right. Because if I want to call do print or a index over here will be equal to one, instead of zero. 225 00:28:35.280 --> 00:28:42.630 William Cheng: Okay, so therefore, when I tried to call this function. I'm going to load are two with our one is the base address and do printers or index number one. 226 00:28:42.810 --> 00:28:53.730 William Cheng: I'm going to end up loading 11,000 over here into our two and then I'm going to call function or two over here using register indirect addressee right so therefore I'm going to end up calling this function at virtual address 11,000 227 00:28:55.290 --> 00:29:02.610 William Cheng: Okay, so you can see that full process. A, it will be able to call printed a lot of virtual address 10,000 and called do print a virtual dress 11,000 228 00:29:02.970 --> 00:29:16.680 William Cheng: Okay, what about for process be right process be over here again we run the same compiler, we generally exactly the same position and independent go. So over here, the code will look exactly the same as process. A. The only difference is that this table has different values in them. 229 00:29:17.520 --> 00:29:21.840 William Cheng: Okay, so this Valley has 20,020 5000 while the other one has 10,000 11,000 230 00:29:22.020 --> 00:29:27.360 William Cheng: So for process be over here when you try to call printer, what will happen, right. So again, here's our to register over here. 231 00:29:27.510 --> 00:29:35.850 William Cheng: We're going to load our to with our one as a base address to this table of your printer is a rating that zero, we're going to copy 20,000 into our to right 232 00:29:36.030 --> 00:29:40.440 William Cheng: And then when I say call are two, so therefore end up calling printer function of virtual address 20,000 233 00:29:40.650 --> 00:29:46.590 William Cheng: OK, and now we're going to continue to execute over here as process be right so process me over here with each other, call do print 234 00:29:46.770 --> 00:30:00.840 William Cheng: What he will do is it will load our to register with our one is the base and do printers are in our index number one. So now our to register over here will contain 21,000 and then when we call call our two over here we're going to end up calling do prevention of virtual address 21,000 235 00:30:01.890 --> 00:30:10.920 William Cheng: Yeah, so you can see that for process and process, be they all have exactly the same Co. But when process a run all the functions that are shared library. They all have different address 236 00:30:11.280 --> 00:30:25.080 William Cheng: Compared to process be again. They will also get a different set of virtual dresses. Okay, so you can also see that the power of India, Russia, if I use the register in direct call here every process will be able to call the same function using different set of virtual addresses now. 237 00:30:26.670 --> 00:30:33.570 William Cheng: All right, so, so. Okay, so this is sort of gave you the idea of how this can be done if you look at a text mode that pixel actually going to give you 238 00:30:34.020 --> 00:30:36.930 William Cheng: A lot of details on this one is done, I think. 239 00:30:37.710 --> 00:30:44.520 William Cheng: So, so, by the way over here. Also, we're going to change the notation over here, right, our one over here is the array index right over here and they use a range like zero and one. 240 00:30:44.730 --> 00:30:49.950 William Cheng: Well we solve for Intel. I mean, they arrange it doesn't really work that way. Right. The way things are done is that we 241 00:30:50.250 --> 00:30:57.810 William Cheng: Are the expression. It's like ep, ep, you know, minus five or something in my mind is for something like that. So we can actually, you know, change them to a more reasonable. 242 00:30:57.990 --> 00:31:09.840 William Cheng: You know location over here where we're going to mention a register over here like Intel and then we say plus or minus, like every entries over here is four bytes law for print out this will be plus zero, y for to print over here will be plus four. 243 00:31:10.290 --> 00:31:17.280 William Cheng: Okay, so you can actually see that these instruction can be compiled and then we'll be able to make a registered in direct call. Okay. All right. 244 00:31:18.660 --> 00:31:28.710 William Cheng: So in the textbook or up here. Again, there's more detail. There are different table, you have to set up and then they watch your show you a bunch of machines structure. So, but execute those machines structure in there will be similar. 245 00:31:29.310 --> 00:31:39.690 William Cheng: Similar to these instructions. And this way you can actually perform you know registering direct call of all these virtual addresses, guys. Oh, yeah. Well, you're not going to go over the detail of how to actually fill these tables. 246 00:31:40.290 --> 00:31:50.280 William Cheng: So again, there are, they're not that interesting. Once you see one assembly code. The you can understand all the other assembly code. Yeah. Alright, so I got it, you're not responsible for this as far as exam is concerned. 247 00:31:51.450 --> 00:31:59.430 William Cheng: All right, so, so, so, so now we know how to actually implement position independent goal. So typically what happens in all the system in Windows and Linux and Unix is that 248 00:31:59.850 --> 00:32:05.910 William Cheng: When they try to use your library. Okay, they will use position dependent call. So this way when you try to compile your code. 249 00:32:06.360 --> 00:32:17.400 William Cheng: You don't need to make a copy of printed and put into your executable. So there's only one copy on the desk and will you run your program again you will be able to share the same code is that memory. All you need to do is to build that table. 250 00:32:18.450 --> 00:32:23.940 William Cheng: Right, the table that we show before we hear these table needs to be built, not a compile time but I run time. 251 00:32:24.960 --> 00:32:29.010 William Cheng: Okay, so that's why this is called dynamic library because they are done. You know what you're running your program. 252 00:32:29.640 --> 00:32:38.880 William Cheng: Okay, so at runtime. You know, when will you load this video library, you're going to go ahead and fill out this table, figure out where all these things are and then you'll be able to actually use all these values right 253 00:32:40.680 --> 00:32:49.530 William Cheng: Alright, so again, there's a lot of detail. We're going to sort of leave all the details out because I guess some people actually try to study, study the code and kernel three I try to sort of figure out how we mix does that 254 00:32:49.950 --> 00:32:56.550 William Cheng: So if you want to do that. It's fine. Or, especially after the semester is over, you want to really find out how this works. So again, there's a lot of coterie yeah 255 00:32:57.540 --> 00:33:06.630 William Cheng: Alright, so the shared library or extensively using many modern operating system. All right, windows, call it dynamically linked libraries or DLL is you probably heard of the house. 256 00:33:07.110 --> 00:33:12.060 William Cheng: So Windows actually mean windows actually use it extensively everywhere. Everything is a DLL 257 00:33:12.750 --> 00:33:16.950 William Cheng: There was, there was a time when they actually sort of they try to do something called distributed objects. 258 00:33:17.340 --> 00:33:23.850 William Cheng: In distributed object or they started out with DLL and then they have something called oh CX and then they have the the active X control. 259 00:33:24.270 --> 00:33:31.200 William Cheng: So a lot of stuff. They have different names, but they're all dynamic a little library. Okay, so, so, so I guess windows that you use them all over the place. 260 00:33:31.620 --> 00:33:36.900 William Cheng: For Unix. They are no assured objects. So the phone and extension over here is going to be so. 261 00:33:37.800 --> 00:33:49.800 William Cheng: Okay, so, so, so the library Commission over here is going to be start with live, followed by the name of the library and then followed by that. So that will be the dynamic library and then again the same name over here without a that will be the static library. 262 00:33:50.430 --> 00:33:56.100 William Cheng: Okay, so when you perform Lincoln, what it will do is that, you know, depends on what kind of linking you want to do if you want to do dynamic Lincoln. 263 00:33:56.250 --> 00:34:00.960 William Cheng: what he would do is that I will look for this dynamic library. If you want to do static linking then it will look for library like this. 264 00:34:01.950 --> 00:34:08.490 William Cheng: Okay, so when you try to compile your program. So, so by default we compile your code in Linux. What they will do is that they will actually try to 265 00:34:08.940 --> 00:34:15.330 William Cheng: Look for the study the dynamic library. First, if they can find a dynamic library, they will use it. Okay, otherwise they will try 266 00:34:15.870 --> 00:34:21.180 William Cheng: To see they can actually use the static library. Okay, so please remember that the dynamic library. The study library. 267 00:34:21.390 --> 00:34:30.660 William Cheng: You got to use that very, very carefully. Once you start using position independent coffee from that point on, every one of your call needs to be position independent right inside the shared library. 268 00:34:30.990 --> 00:34:41.130 William Cheng: Okay, if you need to call function within your executable file, you know, then you actually have a choice whether to implement them using you're using standard library or using dynamic library. 269 00:34:42.150 --> 00:34:44.700 William Cheng: Okay, so, so, because you know if it's a function within your, your 270 00:34:45.810 --> 00:34:51.480 William Cheng: If it's a function inside your executable. Well, then you're not sharing that with anybody. Why so therefore, in that case, there's no requirement for you to use. 271 00:34:51.720 --> 00:35:02.190 William Cheng: Because you need to go. Yeah. But once you start calling see a function inside of C library. If you use positioning the pentacle well then you know the inside to see every everything everything over there will be a will be using position. 272 00:35:03.240 --> 00:35:03.510 William Cheng: Yeah. 273 00:35:05.310 --> 00:35:07.320 William Cheng: All right. Ah, so, so, so 274 00:35:07.590 --> 00:35:19.440 William Cheng: So, so these are the show libraries are, they are not loaded. When a program get started right so if you are studying library problem is that link time we copy all the executable code and also you know the the tech segment of data segments VSS admin. 275 00:35:19.710 --> 00:35:25.500 William Cheng: Into your executable file. Okay, so therefore we you start running your program you actually, you don't need a library anymore. 276 00:35:26.490 --> 00:35:33.600 William Cheng: Right. She doesn't like the content library has the copy into your executable copy into your executable file at the time of compiling and linking 277 00:35:34.440 --> 00:35:38.940 William Cheng: Okay, so this way you're you're executable program will be able to run all by itself without any libraries. 278 00:35:39.360 --> 00:35:44.160 William Cheng: Okay with dynamic library is going to be very different, right, because at the time when we compound do Lincoln. 279 00:35:44.340 --> 00:35:51.570 William Cheng: We just make references to the dynamic library. We don't really copy the code. So when we try to run our program. We need to be able to load these library. 280 00:35:51.660 --> 00:35:57.990 William Cheng: On the fly when we're running our program going to load them into memory and then we're going to, you know, build that table. So we can actually make these positions dependent God 281 00:35:58.950 --> 00:36:09.330 William Cheng: OK, so one very different implication is that if you are using static library you can actually compare some code, you can email it to your friend. And if your friend is running Ubuntu 16.04 and 32 bit one is 6.04 282 00:36:09.600 --> 00:36:13.620 William Cheng: They will be able to run run the program that you that you said on their machine. 283 00:36:14.610 --> 00:36:25.200 William Cheng: Okay, but what if you use dynamic library. What if you use dynamic library. What then you got to make sure that your friend has exactly the same dynamic library in the system, otherwise well you tried to run their program. It will not work. 284 00:36:25.590 --> 00:36:32.760 William Cheng: Okay, so over here says you rather than the disadvantage of the dynamic loader library or share objects over here is that they have dependencies. 285 00:36:33.210 --> 00:36:40.800 William Cheng: Okay, so for example you know my program over here on my progress over here, right. It depends on maybe live prayer, one does. So over here. 286 00:36:40.980 --> 00:36:53.640 William Cheng: Yeah. So in this case, if I say my program to my friend, my program will run the program that will say, Hey, you know, I don't have this library. So therefore I cannot run this program there and then you then your friends will ask you to email you my program. 287 00:36:54.900 --> 00:37:01.110 William Cheng: So like we have here. And then once you friend runs it you know you will be able to run but message look around and say, oh, you know, maybe you forgot something library. 288 00:37:01.290 --> 00:37:09.780 William Cheng: And they are you going to send it. The, the live my pushing that you know that so to your friend, and now you Franklin can actually run run run the program correctly. 289 00:37:10.290 --> 00:37:17.160 William Cheng: Okay, so these library has dependencies. Right. The program depends on the prove one of the proven depends on you know if you have a special version of yours. 290 00:37:17.820 --> 00:37:25.620 William Cheng: You're pushing library. Why, in that case, it would depend on the library. Okay, so in order for you to run your program. All the library depends on the better, I'll be present. 291 00:37:25.980 --> 00:37:28.560 William Cheng: Okay. Otherwise, you know, think it's not gonna work. Okay. 292 00:37:29.220 --> 00:37:36.450 William Cheng: So once in a while. If you get your friends send a program to you and you start running. So your system and you find out that it doesn't work, or maybe this is the reason, okay, because they're using dynamic library. 293 00:37:37.290 --> 00:37:46.920 William Cheng: Now what's even worse is that for that every library. They can be different version of the same library. Okay, so what does that mean a different version, right, if you look at the slash live 294 00:37:48.870 --> 00:37:55.890 William Cheng: I don't remember on Linux. I guess there's something culminates or something like that. There's a Linux directory instead 295 00:37:56.610 --> 00:38:05.070 William Cheng: He is a slash lip, they have some dynamic library of here and you will see that the C library is called lips see thought so. 296 00:38:05.430 --> 00:38:10.380 William Cheng: Too, or something like that. Okay. And sometimes they're actually have longer name. Let's see that so.to dot five 297 00:38:11.250 --> 00:38:19.170 William Cheng: Okay, so this one is I tell you that this. The other that this particular as I live. See that so that to death I so the C library can actually have multiple versions. 298 00:38:19.860 --> 00:38:21.090 William Cheng: Guys, in this case, you know, 299 00:38:21.450 --> 00:38:29.310 William Cheng: So maybe the program that you compile can only work with version two. So when you send your file over here to your friend, your friend also has other the C library. 300 00:38:29.460 --> 00:38:39.030 William Cheng: But they are a version 1.21 in that case 1.2 over here is actually not compatible 2.5 so again we try to run it, then you know the then it will refuse to run 301 00:38:39.600 --> 00:38:43.320 William Cheng: That. So actually, Windows get really, really bad reputation in the 90s. 302 00:38:43.710 --> 00:38:51.840 William Cheng: Because they cannot really get this to to to work correctly. So what happened is that a lot of times people will send their windows program around and then when you try to install the windows program in another 303 00:38:52.230 --> 00:38:57.210 William Cheng: Another person's machine. What they will do is that it will pick up the dynamic library of the wrong version. 304 00:38:57.420 --> 00:39:05.190 William Cheng: And then when you start running it, you know, maybe some of the functions are still present at the shared library, but in that case, you know, the array index is going to be messed up. 305 00:39:05.550 --> 00:39:11.040 William Cheng: Okay, so in that case you think that you're calling one function, but it's true that you're calling some other function and you can end up with the operating system crash. 306 00:39:11.760 --> 00:39:17.820 William Cheng: Okay, so that's one of the reason again windows get really, really bad reputation because they couldn't manage the version you know 307 00:39:19.530 --> 00:39:25.950 William Cheng: The version of the dynamic loading library or inside window. Yeah, so you're exactly the sort of figure out this problem, long time ago. 308 00:39:26.340 --> 00:39:33.330 William Cheng: So the units convention is that whenever you need to create a dynamic library, you better specify a major version number and a minor version number 309 00:39:33.630 --> 00:39:45.960 William Cheng: Guys over here at the first number of years. The major version number, right. So this is a major version number two and minor version number five. The basic idea over here is that if there, if you have to library. There are the same major version number, then they are compatible. 310 00:39:47.460 --> 00:39:56.280 William Cheng: Okay so version one and version two over here. They are incompatible right but if you have version, you know, lipsey so.to dot seven, it's going to be compatible with to Delphi. 311 00:39:56.730 --> 00:40:04.320 William Cheng: Well, so in this case what will be the difference, right. So they have the same you know major version number. That means that the function interface. They're all exactly the same. 312 00:40:04.770 --> 00:40:14.550 William Cheng: Okay. The only difference is over here that you know maybe version seven over here contain bug fixes or maybe they have performance improvements, so you know version seven over here will be a better version of version five 313 00:40:14.880 --> 00:40:25.140 William Cheng: Guys should you use version five well unless version five, you know, there's a bug. So therefore, in that case, you shouldn't use version by but if virgin fiber is just slow wanting that case well using version seven seven and five, both of them is going to be okay. 314 00:40:26.190 --> 00:40:33.000 William Cheng: Okay, so therefore it's so actually this bird is following the programmer, the programmer needs to make sure that when they generate these library. 315 00:40:33.120 --> 00:40:43.260 William Cheng: They need to follow this rule to say that if the functional interface, they're exactly the same. You have to keep the same major version number and using only minor version number for bug fixes and performance improvements. 316 00:40:43.620 --> 00:40:47.490 William Cheng: Okay, so in this case if your library. Right. Typically the library has a header file. 317 00:40:47.700 --> 00:40:56.190 William Cheng: If you change the head of our by adding functions into or by changing the function prototype or in that case you have to go from one version to the next version. 318 00:40:56.430 --> 00:41:03.840 William Cheng: Right. So let's say that you have to use the library and now you add a function to it or you change the function for that one of them. So next version has to be version 3.0 319 00:41:04.800 --> 00:41:14.400 William Cheng: Okay, to make sure that version 3.0 over here, cannot be picked up as if they are compatible with version two. Because clearly, you know, the array index will be off you because you asked new function to it. 320 00:41:14.970 --> 00:41:18.870 William Cheng: Okay, so in that case you you've used the position dependent go, you're going to end up calling the wrong function. 321 00:41:19.920 --> 00:41:27.690 William Cheng: Okay. And also if you change the function prototype and now previously you call the function with For argument's now you want to call the function with three arguments. Well, in that case. 322 00:41:28.050 --> 00:41:37.920 William Cheng: You know, if somebody actually think that they are compatible. They're going to pass you for argument. But as it turns out, the fourth argument you ignore it. Okay. War, the thumbs to the order or arguments or change. Well, in that case is going to be in big trouble. 323 00:41:39.120 --> 00:41:47.790 William Cheng: Or so, so, so again if you if you're using dynamic library, you better make sure that you are completely be aware of the Virgin problem, otherwise you're going to end up with a big mess. 324 00:41:48.060 --> 00:41:54.930 William Cheng: Okay. And again, the responsibility over here falls on the programmer to make sure that their discipline, so that they will name the functions correctly. Okay. 325 00:41:56.460 --> 00:42:04.350 William Cheng: All right, so I guess I was sort of I'll need to finish the rest of the shared library over here that so so so what 326 00:42:05.070 --> 00:42:13.410 William Cheng: When there's a dynamic library. Go get get get being loaded. Okay, so. So as it turns out that there's something called a dynamic loader of the survey, there's, there's a diner. So 327 00:42:13.770 --> 00:42:15.960 William Cheng: Again, we just when we start running your program. 328 00:42:16.140 --> 00:42:20.430 William Cheng: If it turns out there's a function that doesn't exist, you're going to try things out the current all rise in this case. 329 00:42:20.580 --> 00:42:31.170 William Cheng: The colonel. What it will do is it will actually try to load a dynamic library by running a dynamic library load into memory, figure out where all these actresses and it will build that table for you or that you can actually make an indirect call. Yeah. 330 00:42:31.590 --> 00:42:41.340 William Cheng: So in this case, you know, there is a runtime linker that the the the the that the colonel will invoke and for Linux or the wrong kind of anchor is known as Al di di so 331 00:42:41.910 --> 00:42:50.040 William Cheng: Okay, so, so, yeah, LD is a static linker LD. So it looks like a, you know, sort of, sort of, sort of shared library file. So, this one will be the runtime linker 332 00:42:50.400 --> 00:42:58.560 William Cheng: Okay, so we will be the runtime linker that actually build this table for you. So again, for processing over here in the beginning this table doesn't exist because you haven't load this library yet. 333 00:42:59.160 --> 00:43:03.480 William Cheng: Okay, once you load this library over here where you do that you will build this table right figured out 334 00:43:03.930 --> 00:43:14.280 William Cheng: In this case you will perform relocation and runtime figure out where all these addresses and they will populate this table from this point on the position independent called that you generate a compile time and now they will work. 335 00:43:15.300 --> 00:43:18.960 William Cheng: Okay, before that this table was invalid. So therefore you cannot make this function call. 336 00:43:19.080 --> 00:43:27.570 William Cheng: And as soon as we start using the function where you're going to try this out a kernel and the Colonel, actually, in both the runtime linker and then fix all these things up right will load the dynamic library into memory. 337 00:43:27.840 --> 00:43:32.670 William Cheng: Okay, and then it will build this table for you. And now you'll return back to your user space program and everything over. 338 00:43:33.780 --> 00:43:42.450 William Cheng: There. So there's a way in Colonel three if you're turning the if you turn on dynamic equal to one, you're not going to see any of these code over here because they're all loaded at runtime. Right. 339 00:43:43.980 --> 00:43:51.960 William Cheng: Alright, so again, there's a lot of detail in the extra slides. If you're interested, I should take a look at the more detail and also read the textbook. That's over here, we just skip all the details. Yeah. 340 00:43:52.710 --> 00:43:55.080 William Cheng: Alright, so he's going to co create dynamic library. 341 00:43:55.440 --> 00:44:06.960 William Cheng: For Linux system. You have to specify additional command line options, right, for example, if you want to use position independent Co. You have to specify minus FDIC P. I. C stands for position independent call. So this way. 342 00:44:07.260 --> 00:44:13.860 William Cheng: When I compile my posting Darcy into microsurgery. Oh, he will use position independent code inside my pushing that out. 343 00:44:14.550 --> 00:44:22.140 William Cheng: Okay, and then we try to create a library. You also need to use the minus share flag over here to say that the the output file over here will be using a shared library. 344 00:44:22.770 --> 00:44:33.570 William Cheng: OK. So again, this guy's minus oh, followed by the profile name over here and then it's up to you to use that that so you know filename extension if you want to call the star A, it will be fine. You know, the linker is not going to 345 00:44:34.620 --> 00:44:38.250 William Cheng: Allow you to do that. But in this case, when you try to link with the library, you can end up with a big mess. 346 00:44:38.850 --> 00:44:43.950 William Cheng: Okay, so again the programmers needs to have good habits in order for them to specify the correct file. Yeah. 347 00:44:44.790 --> 00:44:52.470 William Cheng: Right. So now once you do this, right. So now the question over here, become a static library. Sorry, become a dynamic library. So we perform this operation over here. 348 00:44:53.190 --> 00:44:59.940 William Cheng: One over here is going to be a static library and this will be a dynamic library. So we used to compile this everything will be okay. They will actually find all these names. 349 00:45:00.180 --> 00:45:11.400 William Cheng: And when you start running your program you actually get an error message. It said that it doesn't really know where am I pushing you know my my pushing live my pushing that SOS, even though my pushing that so he's in the current working directory 350 00:45:12.630 --> 00:45:20.730 William Cheng: Okay. But you said over here the minus over here says, you know, looking out of current directory. As it turns out, the minus l right here is for the, you know, it's actually for the 351 00:45:21.120 --> 00:45:29.400 William Cheng: For the static linker. Okay, so this one is not for the dynamic linker the dynamic lingers el de de so so at runtime, that then I mean lingo needs to find 352 00:45:29.790 --> 00:45:39.000 William Cheng: The dynamic bigger need to find your dynamic library. Okay, so therefore you need to actually pass a different piece of information that dynamic linker. So this way you will be able to find it there. 353 00:45:39.480 --> 00:45:50.340 William Cheng: There is a program that you can run. It's called the link a dependency analyzer Ltd. So if you, you know, if you get this error message you will run Ltd on the program. And now what it will do is it will tell you, for every 354 00:45:50.580 --> 00:46:01.500 William Cheng: You know, for every library where, you know, where would actually find a library that I guess the name, I cannot remember over here is in slash live. There's an LD Linux that so over here. So again, over here, if you, Charlie. 355 00:46:02.280 --> 00:46:13.860 William Cheng: So, so in this case, you know, I guess for Unix cloudy day. So for Linux is called LD dash Linux is. Oh, OK. So again, this is the runtime library. So inside this code over here, they will have all the code that will actually be able to 356 00:46:14.580 --> 00:46:15.690 William Cheng: Do to do other stuff for you. 357 00:46:16.560 --> 00:46:22.710 William Cheng: Yeah, all right. And also, you can see that the C library over here as lipsey that so dot six again this is major version number six. 358 00:46:22.920 --> 00:46:36.030 William Cheng: And this one link to this library in slash lips es el tee ls. I don't know if it's actually on Ubuntu 16.4 and then there's ice 686 that's your CPU tired and then there's some other directory. And finally, it says lipsey that so that says 359 00:46:37.020 --> 00:46:44.160 William Cheng: All right, so. So when you see you know the the the link dependency analyzer print all these information over here. That means that they are able to find a library. 360 00:46:44.820 --> 00:46:50.520 William Cheng: OK, so the dynamic libraries actually smart enough to be smart enough to be able to find all these library in different places over here. 361 00:46:50.730 --> 00:47:01.620 William Cheng: But in the end, you know, it cannot find my pushing that. So there was, it's not about because you haven't specified where to find this pretty library. Right. So one way is to copy this library into this directory one in that case will be found. 362 00:47:02.130 --> 00:47:06.900 William Cheng: But the other way is actually you can use additional a compile a compile time flag to specify 363 00:47:07.260 --> 00:47:16.350 William Cheng: So, for example, one thing that you can do is that you can use original command to generate the executable file. But India, you need to specify additional information to be passed into the runtime linker 364 00:47:16.740 --> 00:47:28.920 William Cheng: Okay, you can actually see that as the syntax that I hadn't kind of ugly minus capital W lowercase l comma minus our path. So, this you know this. Again, this is how you actually find that we locatable library. The library path. 365 00:47:29.160 --> 00:47:34.170 William Cheng: And then you say you look into the current working directory. Okay, so there are two different syntax. One is like this. 366 00:47:34.710 --> 00:47:38.220 William Cheng: This look kind of weird over here, there's a space right here you can replace it with a comma. 367 00:47:38.730 --> 00:47:42.180 William Cheng: So do you actually look a little bit differently or look like this, right, that will also work. 368 00:47:42.570 --> 00:47:48.240 William Cheng: Okay, so in this case again after you do that you run the dependency analyzer, it will it will be able to find all the libraries. 369 00:47:48.660 --> 00:47:55.110 William Cheng: Okay, it does you tell tell tell the runtime linker to say when you are looking for sure. And I read also look into the current working directory 370 00:47:55.800 --> 00:48:06.090 William Cheng: Yeah. Alright, so this way when you talk about your program, you can see that I compile my co using dynamic library or shared library as in this case it will work exactly the same way as before. Yeah. 371 00:48:08.280 --> 00:48:13.530 William Cheng: Alright, so in order for you to to introduce Virginie into your Sheila Ray because you're required to version of the version, a 372 00:48:13.800 --> 00:48:22.680 William Cheng: There's one more thing that you have to do well you perform the link operation right with a happy to share. You also need to specify what is the, you know, what is the shared library name. 373 00:48:23.160 --> 00:48:31.020 William Cheng: OK, so again at the bottom over here and sort of summarize what are the different names over here, the one with a major version number is called the share the share object name or the 374 00:48:31.290 --> 00:48:37.500 William Cheng: Name. And so in this case I'm going to say that the so name for my library is going to be live my pushing that so dot one 375 00:48:38.430 --> 00:48:47.460 William Cheng: Okay, so this way, anything that's compatible with it will be able to use that night. And then finally, this one will be the output file the Alpha Phi need to specify the spec, the real name of 376 00:48:47.820 --> 00:48:56.910 William Cheng: You know of the library file. So in this case, you will have both the major version number and the minor version number right so the minus over here was a look, they're pushing that so dot one dot zero 377 00:48:57.480 --> 00:48:59.730 William Cheng: There and finally the linker name for your 378 00:49:00.030 --> 00:49:06.450 William Cheng: You know, for for your, for your shared library. So this is the girl, the way again. You say minus L. You know my history and then it will be able to find this 379 00:49:06.570 --> 00:49:11.760 William Cheng: Because what it will do is that it will look for that you're the live at the beginning. Over here at the filename extension is that so 380 00:49:12.750 --> 00:49:19.350 William Cheng: Alright, so again, this is the responsibility of the programmer to specify what is the name and what is the actual filename 381 00:49:19.650 --> 00:49:26.670 William Cheng: Okay, so, so you got to be disciplined enough to to determine whether you to get a new version number or not get a new version number. Yeah. 382 00:49:27.540 --> 00:49:31.290 William Cheng: Alright, so once you do that, everything else will work exactly the same way as before that. 383 00:49:31.860 --> 00:49:38.580 William Cheng: All right. The last thing I want to mention over here is that is dynamic substitution or we talked about substituting how to work in static library. 384 00:49:39.060 --> 00:49:46.440 William Cheng: Okay. So this guy is all we need to do is write our own library that will supersede a function instead of C library you can all you can also do this a runtime. 385 00:49:47.100 --> 00:49:55.530 William Cheng: Okay, so this is called into position into positioning so opens up my program over here is going to call the pushing function. Now we're going to actually add a library. 386 00:49:55.950 --> 00:50:04.170 William Cheng: We're going to live with other dynamic library implementation or pushing function over here, right in front of it. So this way instead of our program calling this function invite your car function. 387 00:50:04.980 --> 00:50:08.970 William Cheng: Okay, so people are taking a security class would know right away that this is a security risk. 388 00:50:09.330 --> 00:50:13.890 William Cheng: Okay, because that means that this program when you try to call this function, I can actually change the behavior that function. 389 00:50:14.040 --> 00:50:22.170 William Cheng: So therefore, if I'm a malicious program what I do. Right. I will intercept your call to this function. And now I'm going to do something malicious like cracking your password or something like that. 390 00:50:22.620 --> 00:50:27.060 William Cheng: Okay, so this actually can be exploited. So I'm going to sort of explain to you with the mechanism. 391 00:50:27.540 --> 00:50:33.450 William Cheng: That we can use in the Linux system. But again, you're not really supposed to do it to do something evil. Right. You can only do this for do something good. Yeah. 392 00:50:34.290 --> 00:50:40.680 William Cheng: Alright, so this is the interpretation Colby here. We're going to provide a rapper to the question function over here. Okay, so this way. 393 00:50:40.890 --> 00:50:43.860 William Cheng: Our program will actually call the pushing function inside a rapper. 394 00:50:44.100 --> 00:50:54.480 William Cheng: That. And then, you know, if I rapper is actually malicious what he would do is that it will actually call the real function into make you think that the you know the program is actually calling the real function. But in addition, you'll do something 395 00:50:55.200 --> 00:50:57.690 William Cheng: Some other thing that may be evil. Okay. 396 00:50:58.320 --> 00:51:07.710 William Cheng: So, so again in window. This is known as the allowed injection and we know that the airline injection is really a really bad thing. So again, you should Google this term out to find out exactly what it does on a Windows system. 397 00:51:08.100 --> 00:51:16.200 William Cheng: So again, lots of attack are using the other. This is the old technique. So on a Linux machine, you are now if you're not very, very careful. This can also happen to you. 398 00:51:16.500 --> 00:51:23.580 William Cheng: Guys, so therefore, when we try to use the Linux VA. We got to have good habits to prevent this kind of stuff from happening. Okay, so we're going to talk about that in. Yeah, okay. 399 00:51:25.020 --> 00:51:37.500 William Cheng: Alright, so the way we implement a rapper function over here right to to intercept this code is that yoga, we need to plan, you know, my pushing function over here and this is our own version. Right. So in this case, we need to call the pushing function dynamically 400 00:51:38.460 --> 00:51:46.260 William Cheng: Because how do I should call a function dynamically right so we hear what we need to do is that we need to ask our dynamic link or to give us the virtual address of the 401 00:51:46.620 --> 00:51:52.860 William Cheng: Of the pushing function inside the C library. OK. So the way that you do this over here is that you call TLC. 402 00:51:53.130 --> 00:51:58.170 William Cheng: So Dr CMT S stands for dynamic library. So this one is the symbol look up, you ask 403 00:51:58.380 --> 00:52:05.520 William Cheng: Me as a dynamic library to say, could you look up the symbol for posting that are downstream rather next argument over here is called archaeology next 404 00:52:05.700 --> 00:52:15.300 William Cheng: Is in that starting for our module look downstream right again, you know, we're compiling some you're looking to the right. So again, this is looking the same direction to look for a module over here the containers particular function. 405 00:52:15.840 --> 00:52:26.760 William Cheng: Okay, so if you can find it returned to us the virtual address over here. Again, there's going to be the virtual address. And then what we can do is that we can actually type cast to the right function prototype. And now can actually call this function, you know, 406 00:52:27.330 --> 00:52:30.510 William Cheng: Or, as you call the function in directly using a function pointer. 407 00:52:31.200 --> 00:52:36.360 William Cheng: Okay, you're colonel colonel three you are using function pointer, you call them, you know, you call them using a different syntax. 408 00:52:36.780 --> 00:52:44.850 William Cheng: But in see over here. There's another way to do it right. Once you have a punk songs. You know, we hear you put a star in front of it and then you give it all the argument you will be able to call that function I indirectly. 409 00:52:45.450 --> 00:52:56.070 William Cheng: OK, so again inside of functional area, we cannot call pull string directly because that will look like a recursive call. So in this case, we're going to ask the dynamic library for help find the virtual address and we're gonna call the function 410 00:52:56.280 --> 00:53:06.360 William Cheng: Indirectly but in this case we're going to sort of announced that we're doing that doing this. So, therefore, we're going to write to standard error to say that we are intercepted by my question, and then we call the actual procedure function. 411 00:53:06.720 --> 00:53:10.830 William Cheng: Okay, so this way you will realize that you know the pushing function has been intercepted. Yeah. 412 00:53:12.300 --> 00:53:20.190 William Cheng: All right, so you can actually try this on a Linux system over here. So in this case, you know, when we run our program over here that's called the pushing function will you run it. 413 00:53:20.550 --> 00:53:24.150 William Cheng: Over here, so. So one thing they can do is that you can do this at compile time. 414 00:53:24.630 --> 00:53:30.300 William Cheng: Because a component absolutely not dynamic Lincoln and loading. Right. So this guy's a competitor gonna say GCC minor. So we're gonna 415 00:53:30.600 --> 00:53:33.480 William Cheng: We're gonna link to our library over here, which is the one that we just saw. 416 00:53:33.660 --> 00:53:37.440 William Cheng: And now we run our program over here, we can see that the function has been intercepted right 417 00:53:37.590 --> 00:53:43.440 William Cheng: Again, in this case, we're doing the static Lee, because when we try to resolve this function we're going to look from left to right and then we're going to 418 00:53:43.560 --> 00:53:50.790 William Cheng: Find pushing this library first. So, therefore, this is going to be the one that we use. OK, and not the one is the C library. The see libraries at the end over here. 419 00:53:51.570 --> 00:53:54.840 William Cheng: Okay, so how do you actually intercept this one at runtime. 420 00:53:55.440 --> 00:54:00.450 William Cheng: Okay. So as it turns out, you have to do additional stuff because otherwise going to be too easy to actually take over somebody machine. 421 00:54:00.810 --> 00:54:10.890 William Cheng: Okay, so in the Unix and Linux system. What you have to do is that you need to specify environment variables, right, you've probably never used any environment variable. So I think one environment variable that we 422 00:54:12.360 --> 00:54:16.890 William Cheng: Can remember if I mentioned that already. But you know when you try to run your, you know, 423 00:54:18.720 --> 00:54:24.690 William Cheng: Incredible three we try to run your user space program you're going to call Colonel exactly right. I couldn't exactly he takes three arguments. 424 00:54:24.930 --> 00:54:30.870 William Cheng: The first argument is the path where the executable right and then followed by the function argument, right, and then 425 00:54:31.260 --> 00:54:39.510 William Cheng: Again, that that was a sort of a they use variable. These are argh variable number arguments and also they also passing the invite environment variable. 426 00:54:40.290 --> 00:54:46.620 William Cheng: OK, so the environment variables are a mapping from a string to another string right over here. There's a once you hobby LD pre law. 427 00:54:47.580 --> 00:54:55.680 William Cheng: Or the pre law can be mapped to a library path. Okay. So in this case, you can say set em VI LT pre law to be a name of a shared library. 428 00:54:56.490 --> 00:55:05.520 William Cheng: Okay, so this way when you start running your program. So our program over here, again, in this case we compile. We don't have to link with my pushing that oh so in this case our program, what we're dealing with a C library. 429 00:55:06.150 --> 00:55:16.710 William Cheng: Okay. And since we're doing dynamic Lincoln Elodie if we specify LD pre law at the time when the dynamic library, try to build that appeal to the symbol table for you, what you will do is that it will look here first. 430 00:55:17.670 --> 00:55:23.190 William Cheng: And so in that case it will look into this library. And then what it would do is they will know this library instead of the one is I see library. 431 00:55:24.090 --> 00:55:27.960 William Cheng: Okay. And then what it, what is it appropriate to sort of build that table pointing back 432 00:55:28.350 --> 00:55:41.640 William Cheng: To the point to all the function inside over here. And now when you put on continue to run, you're gonna end up calling function inside your Linux machine that. So OK. So again, if you run your program over here after you said LD preload to be this library, you will see that, you know, 433 00:55:42.960 --> 00:55:51.660 William Cheng: This will be printed out and before you set up the LDP low right will be here. I run the program before that, you can see that when I call when I run my program over here. 434 00:55:52.080 --> 00:55:58.320 William Cheng: The beginning part over here when I get printed because I'm actually using, you know, using Missy library version of the project, project. 435 00:55:58.830 --> 00:56:08.940 William Cheng: OK. So again, the stream I pass the string to the pushing function. So in this case, this is this the only thing they will they will see. But if you are picked up my version of the present value. And now you'll see additional information. 436 00:56:10.350 --> 00:56:18.660 William Cheng: Okay, so using environment variable, you know, you got to be very, very careful if you set up an environment variable. If you point to a place you know where other people can actually put library in them. 437 00:56:18.840 --> 00:56:21.240 William Cheng: Well, then in that case, that person will be able to take over your machine. 438 00:56:22.140 --> 00:56:30.870 William Cheng: Okay, so you got to be very, very careful one day you will be, you know, maybe, maybe you'll be required to use one of the sheer you know machine inside of Adobe Raza those machines are 439 00:56:31.320 --> 00:56:35.670 William Cheng: B2B SCF one sec, up to. So in that case, you will be running your code with a lot of students. 440 00:56:36.630 --> 00:56:46.170 William Cheng: Also with with with the other system administrator will have a lot of people on those machine. So, in that case what I would suggest is that you know in the middle of the semester, you know, maybe your professor will ask you to install 441 00:56:46.530 --> 00:56:55.980 William Cheng: to to to to maybe run a simulation or you certain kind of kind of package in that machine and then they will ask you to change your login script to include LD pre law or LD library path. 442 00:56:56.790 --> 00:57:05.160 William Cheng: Okay, LD library path. It works similar to how the pre law. So you can actually set LD library path and then you followed by a bunch of library with a colon in between. 443 00:57:06.060 --> 00:57:14.670 William Cheng: That. So if you use LD library path anytime. When the linker when the dynamic lingo tried to look for a particular function what he will do is that they will look into this directory first 444 00:57:14.940 --> 00:57:21.180 William Cheng: Guys, and this guy. Typically, one of the directory over here will be the useful program that the professor wants you to to use for that particular class. 445 00:57:21.390 --> 00:57:27.030 William Cheng: What you should do is that at the end when the semester is over, always return your login stroke square back to the original state. 446 00:57:27.510 --> 00:57:31.350 William Cheng: Okay, so this way you won't pick up any extra library and this way, nobody can will be able to attack you. 447 00:57:31.620 --> 00:57:40.920 William Cheng: Okay, if you use our the pillow LD library, you're basically asking for trouble because somebody can actually pull them in English. Let's just malicious library into it and they have a function called printer. 448 00:57:41.610 --> 00:57:50.640 William Cheng: Okay, so this way every time you call up somebody else's code is going around. Okay. And then at the end, they will also called printer make everything look correct in the middle there actually cracking your password to all kinds of nasty stuff. 449 00:57:51.810 --> 00:57:57.120 William Cheng: Alright, so again, when you're using a Unix and Linux system. You gotta be. You gotta be careful with environment variables there. 450 00:57:58.170 --> 00:57:58.770 William Cheng: Alright. 451 00:58:00.000 --> 00:58:05.040 William Cheng: So here's a summary of the you know the dynamic and static library. So again, so we're done over here. 452 00:58:05.610 --> 00:58:12.270 William Cheng: Okay, so we're finally done with the we're done for the entire semester, I just want to, you know, remind you that the final 453 00:58:12.630 --> 00:58:18.210 William Cheng: The final exam I talked about in the beginning of this lecture. Right. So again, we're gonna have we're gonna actually have a take home exam. 454 00:58:19.080 --> 00:58:31.050 William Cheng: Okay, and then I mentioned about the what the exam times are there, they will be posted to the on the class website. So again, it's very important for you to find out which section you'll register for and you take the final exam, right. I can't 455 00:58:31.500 --> 00:58:33.900 William Cheng: Say enough times how important that is. Yeah. 456 00:58:34.350 --> 00:58:41.130 William Cheng: All right. And also, you know, the, the, you know, we will cover a lot of suffering, this class. People always ask me, what's important, what's important. Everything's important 457 00:58:41.370 --> 00:58:48.840 William Cheng: OK, so the final exam covered over here, it does not overlap with the midterm coverage. Right. So the final exam. We're going to cover everything that the midterm hasn't covered 458 00:58:49.200 --> 00:58:54.690 William Cheng: Okay, and also on topics that we cover in the lectures, except the one that we marked with the gray x 459 00:58:55.080 --> 00:59:05.340 William Cheng: OK. So those are all the material they really are responsible for. And again, since the second half of the semester depends on the first half, I cannot say that I will ask anything that was covered that. 460 00:59:05.790 --> 00:59:14.910 William Cheng: I cannot say that I will not. I will not ask any question that will cover in the minimum coverage, right, because it's because, you know, I might accidentally ask something that you should know from the first part. 461 00:59:15.300 --> 00:59:21.900 William Cheng: Okay, so therefore it will be more appropriate appropriate for me to say that the final exam will focus on the material not covered by the material. 462 00:59:22.410 --> 00:59:30.000 William Cheng: Okay, so this way if I accidentally asked you, something that looks like a, you know, the minimum coverage. A question I'm being protected. 463 00:59:30.540 --> 00:59:39.870 William Cheng: Okay, you are not you cannot say that I'm not asked a question. Okay. Because over here. I said, I'm going to focus on the second part. But even though once in a while, I might end up asking a question, the first part that 464 00:59:41.280 --> 00:59:53.310 William Cheng: Alright, so, so this is the final week right this is week number 15 on next week. There's no office hour. There's no you know zoom or anything like that. So if you have questions about the lecture material when you are preparing for the final 465 00:59:53.580 --> 00:59:59.550 William Cheng: Make sure you send your questions to me via email. Okay, I will not have a review session, I don't have anything like that. Right, so, send me email. 466 01:00:00.180 --> 01:00:05.910 William Cheng: Me know. So again, just like the midterm rule over here right you know by the time exam starts starts 467 01:00:06.120 --> 01:00:12.990 William Cheng: Anything that's on the lecture slides on a textbook. Those are the correct answer, right, if you don't agree with this thing on this lecture slides on the in the textbook. 468 01:00:13.200 --> 01:00:24.720 William Cheng: You need to challenge me before the exam star and if it turns out that you're right and I was wrong. I need to send a message to the entire class to say now, you know, this particular slide I'm changing it. I'm changing from this does this and now there's a new answer. 469 01:00:25.680 --> 01:00:33.030 William Cheng: Alright, if I don't do that. Okay. So then, then you have what's on the lecture slides, what the textbook. They are the correct answers now. 470 01:00:34.200 --> 01:00:44.970 William Cheng: Or I'm some, some people do that. And then, in the meantime, after the exam over, they say, oh, I don't really like that you know that that you're answering the lecture slides. It's too late. Okay, you have to cut you. You have to talk about this before the exam starts. Yeah. 471 01:00:45.810 --> 01:00:54.390 William Cheng: Alright, so again, I need to apologize for going so fast, especially at the beginning of the semester, you know, and later on I sort of slow down a little bit, but still have to talk pretty fast. 472 01:00:54.750 --> 01:01:02.850 William Cheng: Because in the end, this class we have so much to cover. Okay. But you can see that, you know, India, even the last lecture over here. Go over 80 minutes, there's really not much time left. 473 01:01:03.390 --> 01:01:09.390 William Cheng: The only way I can slow down to cut lecture material, but every semester I try to cut something. But in the end, there's really nothing to cut 474 01:01:09.960 --> 01:01:14.130 William Cheng: Okay, because I think all the stuff that we cover in this class. They're all very important topics. Okay. 475 01:01:14.730 --> 01:01:24.330 William Cheng: All right, so if you have question, you know, send me an email. Otherwise, I'm going to see you. I won't see you I will continue to, you know, so they exchanged email with you. Okay. All right. Good luck at the final exam, right.