WEBVTT 1 00:00:02.159 --> 00:00:14.280 William Cheng: I lost the internet connection, towards the end of the previous video. So now I'm going to record the fourth video just to finish, you know, 2 00:00:15.000 --> 00:00:25.500 William Cheng: Just to finish the last topic over here. Okay, so we just saw that you need to invoke out the the. So, and that's the dynamic, you know, linker 3 00:00:26.100 --> 00:00:33.810 William Cheng: So dynamic winter they perform you know relocation and then you actually build those data structure and then it will let the product contingent right yeah 4 00:00:34.170 --> 00:00:39.660 William Cheng: Alright, so how do you create a dynamic library or how do you create a shared library. So on Linux machine. 5 00:00:40.260 --> 00:00:51.630 William Cheng: If you want to instruct the compiler at the January position independent Co. So the way you do this is I use a GCC minus F. R. I. P. I. C. A. P. I see. Of course, this will position independent co 6 00:00:52.080 --> 00:01:01.530 William Cheng: So this way you know inside the library about, you know, you want to make sure everything the code that the generator is using the registration direct call now. 7 00:01:01.920 --> 00:01:07.380 William Cheng: And will you try to create the the the shared library file, you have to use the minus shared 8 00:01:07.950 --> 00:01:14.520 William Cheng: Command on argument. So in this case, you also need to make sure that the Father you generate use the data so far then extension. 9 00:01:15.060 --> 00:01:27.540 William Cheng: So as it turns out that you know in Windows or the father of extension, it means something in window and the Linux or Unix system. The phonic extension doesn't mean anything. So it's up to the programmer to make sure that they have good habits. 10 00:01:28.080 --> 00:01:36.180 William Cheng: To name the dynamic library with that so extension and named the static Rebel Without a doubt about it on extension. Okay. You can name these any way you want. 11 00:01:36.750 --> 00:01:43.680 William Cheng: But you got to be disciplined enough of us. So, followed by live. You got to follow the naming convention. So this way you can generate that library. 12 00:01:44.340 --> 00:01:50.580 William Cheng: Now, so everything else is the same over here. So what happened is that what you finished running a program up here. We try to run the program over here. 13 00:01:50.970 --> 00:02:01.860 William Cheng: It turns out that, you know, even though you use exactly the same command and compile and linking actually works when you try to run the program, it will say that it doesn't really know you know how to find the stream library. 14 00:02:02.340 --> 00:02:13.020 William Cheng: Okay. But over here. It says, I use it. I mentioned that is our current working directory over here. That's where you find the push pushing directly as it turns out that the minus capital L this is only for static Lincoln and loading. 15 00:02:13.560 --> 00:02:17.190 William Cheng: Okay, so therefore, at the time when you try to compile the practices. I found this program. 16 00:02:17.670 --> 00:02:23.760 William Cheng: But again, all the names are unresolved when it's tried to run the program in that case it doesn't look into the current working directory anymore. 17 00:02:24.180 --> 00:02:36.810 William Cheng: So if you want to run it and if you try to be able to find this this library. I run time. You need to do something else. Now, so, by the way to run a program. This is called the Ltd program. 18 00:02:37.440 --> 00:02:48.390 William Cheng: The theory is that dependency analyzers. So this one is for LD, but it's the analyzer dependency. So if you'd want it on the the program, we're here you will see that at runtime, it will not be able to find to find this particular library. 19 00:02:48.780 --> 00:02:57.180 William Cheng: Okay, all the other library will be able to find out, for example in live see that so that six it finds in this directory and then the LD Linux that so so again this is the 20 00:02:57.750 --> 00:03:03.960 William Cheng: You know, the, this is a dynamic link or are they actually find the right here, LD Linux. So this case is version two. 21 00:03:04.770 --> 00:03:06.540 William Cheng: The only thing I can apply as a pushing library. 22 00:03:06.930 --> 00:03:17.550 William Cheng: Okay, so what happened is that at compile time. You need to tell it to pass some information to the runtime linker and and and this, in this case, you want the runtime link go to find the dynamic library in the current working directory 23 00:03:18.240 --> 00:03:20.250 William Cheng: Yeah, so this is kind of a weird syntax. 24 00:03:20.940 --> 00:03:28.530 William Cheng: In Linux, you have to, you know, all the stuff that you said before over here is same as before, but now you need to provide additional information to be passed to the runtime thinker. 25 00:03:28.710 --> 00:03:35.400 William Cheng: So you say minus capital W lowercase l comma no space minus our path and then you followed by the period. 26 00:03:35.790 --> 00:03:40.050 William Cheng: There are two different syntax. One is this address. In fact, the other one is that you can add a comma right here. 27 00:03:40.410 --> 00:03:47.370 William Cheng: So we will look at one command argument. So this is how the runtime naked to say when you try to look for the dynamic library also 28 00:03:47.850 --> 00:03:50.250 William Cheng: You know, low for them. He started working directory current working directory 29 00:03:50.520 --> 00:04:04.140 William Cheng: Yeah, so if you do this, and then you run the other dependency analyzer. Again, they will say they will actually find all the program and we start running at you'll see that the producer producer, that the desired output in this case is done with using dynamic library. Hey, 30 00:04:05.250 --> 00:04:11.580 William Cheng: All right. And the other thing is version eight. So I mentioned that you know there's major version number and minor version number so the 31 00:04:12.060 --> 00:04:21.090 William Cheng: Unix and Linux convention is live. I followed by the name of the library, followed by that. So, and then followed by the major version number followed by the minor version number 32 00:04:21.570 --> 00:04:27.540 William Cheng: Okay, so, so, so, so again, these kind of convention. If the programmer need to be very careful the program and nice to 33 00:04:28.020 --> 00:04:34.860 William Cheng: Meet you determine when to, you know, pump the major version number and went to increase the minor version number, the compiler doesn't really try to help you. 34 00:04:35.280 --> 00:04:44.220 William Cheng: Okay, here are some of the terminology over here. The one was only the major version number, you know, these are the one that all the libraries are compatible. So it doesn't know as the so name of the share object name. 35 00:04:44.760 --> 00:04:50.880 William Cheng: The one with the major version number and the minor version number. So this one is you specify, you know, which specific implementation. 36 00:04:51.120 --> 00:04:57.330 William Cheng: Of this library. So this is called the real name. And if I don't leave the one without the major and the minor version of what this is all an awesome synchrony. 37 00:04:57.840 --> 00:05:02.940 William Cheng: Well, so when you try to create. So if you want to use version me the additional information that you have to specify 38 00:05:03.480 --> 00:05:13.800 William Cheng: You know, you need to add the minus. So, to emphasize what is the so name again the so name over here is one with the major version number. So you need to copy all this kind of stuff. And you should add a dot followed by the major version number 39 00:05:14.220 --> 00:05:20.040 William Cheng: After it. Okay. So even though it sounds really silly. Why shouldn't the link over here is figured out that you know from the 40 00:05:21.540 --> 00:05:29.100 William Cheng: From the library file just add one to it will remove the the minor version number and they will give you the same information, but he's not case again for Linux. 41 00:05:29.400 --> 00:05:40.050 William Cheng: The other programmers forced to do this. Yeah, and also the output file over here is going to be the real name. So it needs to have both the major version number and the minor version number. Okay. Everything else is all over your body exactly say yeah 42 00:05:41.850 --> 00:05:52.590 William Cheng: Alright. Finally, what if you have a function that that that that you want to take over the implementation we saw in the static library is no substitution 43 00:05:53.310 --> 00:05:56.370 William Cheng: In the dynamic library, they have a different name. It's called into positioning 44 00:05:56.790 --> 00:06:05.550 William Cheng: OK. So the idea of years, I have a program over here is going to call this function dynamically. So what I want to do is that at the time when this function is gonna call with a program called associate 45 00:06:05.760 --> 00:06:14.670 William Cheng: I'm sort of going to intercept it. I'm going to replace it with my own version of the function is that a C library. Okay, so some people you know if you have any 46 00:06:15.390 --> 00:06:21.270 William Cheng: If you have any kind of a security staff, you'll say, hey, this is really dangerous because at runtime, you can actually intercept a library call 47 00:06:21.420 --> 00:06:28.680 William Cheng: And replace it with you know your own implementation. Why can't you, you know, guess people's passwords or try to intercept the other function call and, you know, 48 00:06:28.920 --> 00:06:33.690 William Cheng: You know when you try to verify your password. You can also see all that information, right. So again, this is a major risk. 49 00:06:34.140 --> 00:06:40.620 William Cheng: Okay, but since we need. We were providing study in your static substitution in dynamic library. You also need to provide the same thing. 50 00:06:41.220 --> 00:06:48.180 William Cheng: Okay, so, so you just have to do this very, very carefully, because otherwise, some other people will be able to take take over your program. Okay, so in Windows 51 00:06:48.540 --> 00:06:58.380 William Cheng: This is known as DLL injection. Right. So if you have taken some database class or some system class. You've probably heard of that term. So again, that's how hacker gay gay gay gay gay gay gay control your program. 52 00:06:59.580 --> 00:07:03.330 William Cheng: So the idea here is that we're going to put a rapper, you know, dynamic library. 53 00:07:03.990 --> 00:07:14.100 William Cheng: Around there and this rapper pumps. You also have the same name function. Right. So instead, you know, instead instead Robinson, we're going to do something different to replace are the functions that are C library. 54 00:07:14.430 --> 00:07:19.320 William Cheng: There. So again, this is exactly the injection windows you gotta do it with care. 55 00:07:19.770 --> 00:07:27.540 William Cheng: The way this is done is that you again, you need to you need to provide your own library and inside your library employee on function over here. So instead of call 56 00:07:27.780 --> 00:07:33.960 William Cheng: You know, instead of, you know, implement this particular function using the right bumps you are we going to do. So we're going to actually call it the actual function. 57 00:07:34.470 --> 00:07:41.280 William Cheng: Okay. So, okay, this is, you know, if you are malicious you know coder or something like that, what you will do is that you will actually called the real function. 58 00:07:41.430 --> 00:07:49.170 William Cheng: But in addition, you going to do something you're going to do something devious, right. So for example, you going to pass the witchcraft password or, or, you know, look at other files or something like that. 59 00:07:49.590 --> 00:07:54.450 William Cheng: So over here, instead of doing something evil, we're going to actually print a message and say, hey, we're intercepting your call over here. 60 00:07:55.080 --> 00:07:58.530 William Cheng: So again, you should use this to this technique, you know, for doing something good and not 61 00:07:59.040 --> 00:08:05.340 William Cheng: Evil. Yeah. So the way this is done over here is that inside this function in order for us to call the real implementation over here. 62 00:08:05.520 --> 00:08:13.590 William Cheng: What we need to do is that we actually need to ask the dynamic Lincoln and load the dynamic dynamic linker to tell us to give us the address for this function. 63 00:08:14.010 --> 00:08:23.970 William Cheng: Right. So the function that you will call. It's called the same dynamic library symbol look up, you say, look up the purse strings symbol, we here inside, you know, by, by looking at the civil downstream. 64 00:08:24.180 --> 00:08:30.360 William Cheng: Inside my list of library over here, find the actual, you know the the actual virtual at virtual address for that function. 65 00:08:30.660 --> 00:08:39.450 William Cheng: better this way. What it does, it will return, return to your function pointer. I'm going to store the function pointer into those local variable be here again typecast using the right function pointer. 66 00:08:39.750 --> 00:08:48.150 William Cheng: You know, a function prototype over. Yeah. And then what I do over here is I'm going to create a message to say that, you know, over here, I intercepted your message. And then what I can do that. I can actually called assumption. 67 00:08:48.990 --> 00:08:56.550 William Cheng: Okay, so in the Unix and Linux system. It provides you with a way to query or to provide a look up. I'm sure it will look up a virtual address of a dynamic function. 68 00:08:57.210 --> 00:09:03.870 William Cheng: Okay, so at this point, you know, this function will be loaded that table will be built. So you can actually figure out what the virtual address, there's a way to call that function. 69 00:09:04.620 --> 00:09:11.340 William Cheng: Now. And so again, you know, you know, you could do this even though you shouldn't you shouldn't do something like that. 70 00:09:12.510 --> 00:09:22.530 William Cheng: Alright, so over here. So if I want to use that particular syntax. I need to have additional a command line arguments over here on, you know, for this to work. And you can see that after I do all this kind of stuff, create a dynamic library. 71 00:09:22.710 --> 00:09:34.020 William Cheng: And now when I run my program over here. So in this example here, I'm almost doing static in Lincoln and loading because what I tried to do that without a compartment program. I say that if you cannot find a symbol look inside the slide refers, and then 72 00:09:34.350 --> 00:09:44.760 William Cheng: You know, looking at the library. So in this case, again, they will find and pushing function inside this library first. Yeah. So that's one way to do it. Okay. The other way to do it is actually using environment variable. 73 00:09:45.330 --> 00:09:56.130 William Cheng: You haven't really we haven't really talked about environment variable, you know, but what you do your kernel three code, you will see that, you know, incredible exactly you need to pass to argument. One is the is the 74 00:09:56.970 --> 00:10:05.730 William Cheng: Is the RV and RC and the other one is a list of environment variables. Okay, the environment variables over here, you can actually set them up inside your login shell. 75 00:10:06.150 --> 00:10:12.450 William Cheng: So this way you can actually, you know, set up something are specific for you. Well, I'm going to talk about two of them. One is called LT preload. 76 00:10:12.900 --> 00:10:22.080 William Cheng: Okay, so you can say set LD preload and then you can actually specify a dynamic library. So, this one says that, you know, when you try to perform that I mean, Lincoln Elodie look here first. 77 00:10:22.680 --> 00:10:28.890 William Cheng: Okay, so this is an instruction for the dynamic thinker and Loader to set it to function. Look, this first before you look at the C library. 78 00:10:29.250 --> 00:10:37.710 William Cheng: So in this case, when we try to run our program over here. You want to pick up the behavioral be intercepted by my pushing and it will print out your this particular this particular message. 79 00:10:38.310 --> 00:10:46.230 William Cheng: Now the other environment variable, you can set is called LD library path. It will specify a path of directory. So it's going to be a list of directory 80 00:10:46.920 --> 00:10:51.450 William Cheng: Directory. So, typically the way the settings work is that he says set LD library path. 81 00:10:51.690 --> 00:11:01.650 William Cheng: The first director that you you need to look for for dynamic library is a user lab. And then you look for inside leg. You can also add additional directory into it. And then the last one over here is the 82 00:11:02.100 --> 00:11:12.960 William Cheng: Is the data entry over here, you look into the current working directory. Okay, all these directories are separated by the colon. So therefore, when the runtime think I need to look for a library. What it will do is it will follow this order and try to look for 83 00:11:15.600 --> 00:11:16.890 William Cheng: To try to look for particular function. 84 00:11:17.520 --> 00:11:24.480 William Cheng: Okay, so if you're taking some, you know, other classes, especially in engineering, maybe, you know, mechanical engineering W E or something like that. 85 00:11:24.750 --> 00:11:32.520 William Cheng: You order for you to use certain piece of software in a shared server what he would do is I in the logging script, they will actually change the login library path over here. 86 00:11:33.030 --> 00:11:41.370 William Cheng: to to to to specify additional library to us. So we should do is that at the end of the semester. If you're using a shared account. So, so are you using an account in a share. 87 00:11:42.090 --> 00:11:48.420 William Cheng: In a shared machine will just you just use do is I, you should always reset, you know, your longest we're back to the original one. 88 00:11:48.870 --> 00:11:59.550 William Cheng: Okay, so this way later on if somebody as some other funny library inside you know inside some of these specialized library. They will not pick up your, you know, the will return on your program, they will 89 00:12:00.240 --> 00:12:06.840 William Cheng: Not going to end up, you know, picking up their version of pushing or their version of printer. So in this case, they can actually be, you know, 90 00:12:07.260 --> 00:12:13.050 William Cheng: In addition to running printer. They can also spawn off a child process, and he will actually, you know, crack your password and things like that. 91 00:12:13.350 --> 00:12:18.750 William Cheng: So in that case, they will be running this other program as you so therefore they have access to all your files. 92 00:12:19.440 --> 00:12:23.400 William Cheng: That. So it's always at the end of the semester, you need to reset the you know the 93 00:12:24.120 --> 00:12:29.880 William Cheng: These environment variable back to the original setting, even though during the semester, you won't have access to additional program. 94 00:12:30.300 --> 00:12:40.230 William Cheng: But the idea for security reasons, you always want to revert back to the standard login script. When all right. Alright, so this is, you know, this is 95 00:12:40.680 --> 00:12:44.940 William Cheng: This is everything that we need to talk about. So we are done for the semester over here. 96 00:12:45.480 --> 00:12:55.440 William Cheng: So I just wanted to mention that the final exam coverage will be posted on the class website and also the final exam or just a all the informations there. So again, look there to get familiar with it. 97 00:12:55.770 --> 00:13:01.470 William Cheng: And the, the cover that the you know the the intention for the final exam is not to overlap with the mature. 98 00:13:01.800 --> 00:13:05.220 William Cheng: Okay, but since the second half of the semester depends on the first half. 99 00:13:05.460 --> 00:13:14.460 William Cheng: I will, you know, I will not be able to say that I will not any question that will cover in the minimum coverage, right, because otherwise, some people are going to say, hey, you know, you ask a question like that. So, therefore, you have to give you full credit 100 00:13:14.850 --> 00:13:26.010 William Cheng: Okay, so therefore, I'm not going to say that I will not use anything from from the first semester, the right thing to say that is that, you know, it will be more appropriate to say that the final example focus on the material that's not covered by the matter. 101 00:13:26.790 --> 00:13:39.060 William Cheng: Okay, so basically you know you can look at the you know the the lectures web site it's after the midterm coverage and plus everything in chapter five. And that will be the final exam coverage. Right. That will be the sort of the focus of the final exam coverage now. 102 00:13:40.320 --> 00:13:52.770 William Cheng: Alright, so this is the final week and also will have no more lecture also no more, you know, discussion session on Friday. And also, this will be the last week that will have the office hours next week. There will also be an office out. Yeah. 103 00:13:54.000 --> 00:14:03.120 William Cheng: So fossil fuel. I need again, I apologize. We're going so fast, you know, because we have so much material to cover the only way I can slow down is to cut down material. 104 00:14:03.660 --> 00:14:06.720 William Cheng: Okay, so every semester I sort of tried to sort of figure out what can cut down but it's 105 00:14:07.560 --> 00:14:17.610 William Cheng: always seems to fail because pretty much every, you know, copy that we cover in this class. They're all important topic. Well, you go interview, somebody's going to ask you these kind of questions and they expect you to understand all these topic fully 106 00:14:18.540 --> 00:14:26.220 William Cheng: Okay, so if you have any suggestions for what I can cut out, you know, feel free to do that. So, so, so then I can review it to see if I can cut down on a full feature semester. 107 00:14:27.120 --> 00:14:33.870 William Cheng: So, otherwise, you know, here's the end of semester. And I just want to say that I really enjoyed teaching this class. I hope you enjoyed the material for this class. 108 00:14:34.080 --> 00:14:39.390 William Cheng: I mean, I think the programming assignments grey from Brown University. I also hope that you have learned a lot from programming assignments. 109 00:14:40.110 --> 00:14:48.330 William Cheng: Most of all, is I want you to, you know, will one day, you gotta go interview and if you get a job offer because of you know what you're learning see us War two. 110 00:14:48.540 --> 00:14:53.100 William Cheng: It will be great if you can send me emails it this way. You know, I can actually feel good about this class. 111 00:14:53.490 --> 00:14:58.620 William Cheng: That's basically, that's my own reward you know sort of my main reward, which in this class is to is to her that 112 00:14:59.010 --> 00:15:01.440 William Cheng: You know, student get great job offers you know because this class. 113 00:15:01.800 --> 00:15:10.740 William Cheng: Yeah. Alright. So when you study for the final exam. The rule again. It's going to be the same as the midterm of your question, you know, send them to me. Otherwise, what's posted in the lecture slides. 114 00:15:10.950 --> 00:15:17.460 William Cheng: Is going to be sort of the correct answer for all for all the final exam questions. Okay. Alright, see you, until finally it