[CS61C FA20] Lecture 13.4 - Compilation, Assembly, Linking, Loading: Linker — Transcript
Full transcript
- 0:00and welcome back now let's see the third
- 0:03of our four part series
- 0:04the linker what does that do for us so
- 0:07where are we now
- 0:08the linker is the third stage the
- 0:10compiler has done its work you've got a
- 0:11dot s file the assembler's done its work
- 0:13it's like i got it.o file
- 0:14you might have a lot of dotto files you
- 0:16might have library.o files or maybe dot
- 0:18a files which are library files that
- 0:19have been wrapped together
- 0:20into a dot a file which is like a big
- 0:22package of them all and the linker's job
- 0:24is to put them all together gather them
- 0:25all together at the table
- 0:27and produce an adot out or executable
- 0:29file
- 0:31so i just said most of this here you
- 0:33take the os from the libraries and from
- 0:35all the user files
- 0:36the goal is to produce an executable the
- 0:38process of doing that is called
- 0:40linking and really the benefit of this
- 0:42you've seen before
- 0:43is many different files can be authored
- 0:47and you don't have to do the whole
- 0:49process of compiling and assembling them
- 0:51if you have them all in
- 0:52os if you only change one file you'll
- 0:53need to change recompile that one
- 0:55reassemble that one and then relink the
- 0:57whole thing together so it's still
- 0:58linking is the kind of bottleneck of the
- 1:02whole process where all of those have to
- 1:03be come together but
- 1:04the compilation and assembly can be done
- 1:06only for the small file that got changed
- 1:08which is really quite nice because some
- 1:09of these
- 1:09some of the source or some code linux
- 1:11photoshop can be really really massive
- 1:13and
- 1:13you make one line change you shouldn't
- 1:15have to do all that work of compiling
- 1:16assembling all of those
- 1:17you only have to relink them and i
- 1:20should say
- 1:20the old name for this was called the
- 1:22link editor because the thing that
- 1:23you're doing is you're fixing all of the
- 1:25links that are on the relocation table
- 1:26relocation table says this is the stuff
- 1:28we didn't know those absolute addresses
- 1:29yet so
- 1:30those are the links that i need to fix
- 1:32and so the link editor linker
- 1:33comes from the name link editor which is
- 1:35you're fixing those links from jumping
- 1:37links
- 1:38so again each.o file has three parts to
- 1:41it the dot text
- 1:42all the code.data which is all the data
- 1:44and all the information all the
- 1:46symbol table relocation table debugging
- 1:48information is in the info
- 1:49every one of the dot os has that they
- 1:51all come together from the linker's
- 1:52point of view and you get an edit out
- 1:54by taking all the dot all the texts and
- 1:57put them together you have some order to
- 1:59them all
- 1:59so you know the food comes before bar
- 2:01comes before library let's say foo bar
- 2:03and some math library okay
- 2:04so it's foo bar math library text foo
- 2:07borrow
- 2:08math library data and then you use the
- 2:11information to fix those links in there
- 2:13so that's the idea
- 2:15so again take that all the text go
- 2:17together in what order put them together
- 2:18concatenate them together
- 2:20put all the data concatenate them
- 2:21together and then resolve those
- 2:22references go through that relocation
- 2:24table
- 2:25find your symbols and figure out what's
- 2:26happening and put in the absolute
- 2:27addresses
- 2:29four types of addressing you've got pc
- 2:31relative addressing we
- 2:32love pc relative addressing because pc
- 2:34relative means it's position independent
- 2:36code pic
- 2:37branch equal branch not equal jowl with
- 2:40au add upper media pc plus add i all
- 2:43those things are relative to the pc
- 2:45and i don't need to worry about that
- 2:46love that
- 2:48the other three though we have to always
- 2:49relocate that's the part the linker's
- 2:51like oh boy i gotta do some work here
- 2:53and that's for any absolute function
- 2:55address so a function call
- 2:56here that's an absolute call not a
- 2:58relative call we saw that before
- 3:00an external function call certainly i
- 3:02don't know what that is i i mean
- 3:03even though it might have been the same
- 3:04call it the same function but if it's
- 3:06referred for if it's referred to based
- 3:08on an absolute function still got to
- 3:09relocate that one
- 3:10if it's an external function if it's
- 3:12foo's call bars function
- 3:13certainly i don't know where bar was in
- 3:15the final resting place i gotta
- 3:17get that guy and any static data if i
- 3:19have any static data that's maybe
- 3:20relative to the static area i don't know
- 3:22where the static area was going to be
- 3:23all that stuff has to be there okay
- 3:25easy so which instructions need
- 3:28relocation editing
- 3:29well jump and link all of those upper
- 3:32bits here
- 3:33need to be modified as a result of that
- 3:37is format this is relative to the static
- 3:39area this global pointer points to the
- 3:40beginning of the static area that says
- 3:42here's where all the static stuff starts
- 3:44well this is all relative to that that
- 3:46static area but since i don't know where
- 3:48the static area is until i finally put
- 3:50it there
- 3:51until i finally have text text text text
- 3:53data data data data that right here is
- 3:55the beginning of the static area that's
- 3:56when i now know
- 3:57uh where that value is and this will be
- 3:59filled in here and this is going to be
- 4:01relative to that these values relative
- 4:02to that
- 4:03how about conventional branches do i
- 4:05need to do any work there
- 4:07no conditional branches are all branch
- 4:09equal and branch not equal
- 4:10these are all these conditional things
- 4:11that are all pc relative position of
- 4:13independent code
- 4:14don't need to worry about that these
- 4:16values would have been filled in there
- 4:17filled in already these are not
- 4:18like x's to do's actually they're
- 4:20usually they set them to zeros but they
- 4:22said as like to do's that
- 4:23the final number is there don't need to
- 4:25worry about those at all i'm pretty good
- 4:27does it start from zero we always talk
- 4:29about the code starts at zero
- 4:30actually the linker assumes the first
- 4:32word of your first text segment starts
- 4:34at
- 4:3410 000 in hex which ends up being 64k
- 4:38if you think about this each of these
- 4:39hex is four bits so that's four bits
- 4:41here four bits here at sixteen so that
- 4:43means this is two to the sixteenth
- 4:45so two to the 16th is 64k so it starts
- 4:47at the 64k place
- 4:48is where the first text segment ends and
- 4:50that's like text one text two takes
- 4:51three
- 4:52data one data two data three and then
- 4:54above that is what
- 4:56sure the heap grows from there and the
- 4:57stack grows in the top down that's the
- 4:59idea
- 5:00the linker knows the length of these
- 5:01guys all the information in the dot info
- 5:03in the information file from each of
- 5:04those
- 5:05files tells you how long those things
- 5:07are so now that
- 5:08now that i know how long they are i'll
- 5:09go bloop bloop bloop put them in there
- 5:12and and actually doesn't actually put
- 5:13them in there until the end but it says
- 5:15okay that's how long they are so let me
- 5:16go now fix those
- 5:18let me fix those links i can i now know
- 5:19an ordering to them all and i can now
- 5:21calculate what the absolute address is
- 5:23and i can now repair
- 5:24and edit those links so to resolve the
- 5:27references
- 5:29i search for a data or label in all the
- 5:32user symbol tables if you're saying i
- 5:33want to load some
- 5:35you know load address well what address
- 5:37that's a label
- 5:38okay well let's look for all the labels
- 5:39and all the symbol tables
- 5:41you can't find it symbol oh no they'll
- 5:43say linker symbol.found
- 5:45if you call a function if you actually
- 5:47joule call some function that's calling
- 5:49some maybe external function maybe you
- 5:52don't have that maybe you didn't include
- 5:53that library maybe you decided to maybe
- 5:56you forgot to include the math library
- 5:58you're calling sign but you didn't
- 5:59include the math libraries there's no
- 6:00math.o at all doesn't even know what
- 6:02math.0 or math.a is
- 6:03doesn't know what that is at all so it
- 6:05looks for sign in all the symbol tables
- 6:06can't find it it'll say symbol unknown
- 6:08that's great this is also the time when
- 6:10if you had conflicting names
- 6:12foo has a function baz bar has a
- 6:14function baz
- 6:16independently compiler assembler didn't
- 6:17know that they were just doing their job
- 6:19independently just doing my job sir
- 6:21down here now i've got two bazes it's
- 6:24two symbol tables now as i'm compiling
- 6:25all the total symbols that are available
- 6:27wait i can't have two bars it'll say
- 6:29conflicting symbol type again that's an
- 6:30area that only the linker knows about
- 6:32only when you see all of them together
- 6:33does i think you know about that once
- 6:35you're all done once you have all the
- 6:36absolutes determined you can now write
- 6:38the final machine code and you're all
- 6:40done machine code is all i was all done
- 6:41so the output of this is an eight out
- 6:45now what i've described so far is a
- 6:48statically
- 6:48linked executable it means you load it
- 6:51on all the data
- 6:52and text and you put it in there it's
- 6:54all in a big ball so if i decided to
- 6:56include the opengl library which is a
- 6:58really big library a lot of code a lot
- 6:59of data
- 7:01well okay i'm putting that in there and
- 7:02then put this if so my a.o starts to
- 7:04grow and grow and grow because it's
- 7:06statically linked it's all linked
- 7:07together
- 7:08in the file that's great love that now
- 7:11all those libraries are part of the
- 7:12executables
- 7:13so if i update the libraries i don't to
- 7:15fix it let's say
- 7:17i have a new library fix well i don't
- 7:20get it i have to then recompile that
- 7:22to uh if i have the source we have to
- 7:25recompile it to get that library into it
- 7:26because if you make a new library change
- 7:28well i this is a baked it i call it
- 7:31baking in we call it in computer
- 7:32graphics and in
- 7:33and in systems i bake it into the thing
- 7:35it's there it's baked in i can't change
- 7:37it
- 7:37um so if i have a new library i can't
- 7:39get that library oh this new library is
- 7:41really
- 7:41ten times faster sorry all my
- 7:43executables were compiled with
- 7:44uh the old library i have to now
- 7:46recompile them that's always a pain
- 7:48right so if there's
- 7:49let's say all the source code for every
- 7:51apple mac os
- 7:52program is referencing one critical
- 7:54library and then it has a fix
- 7:56oh my god every single person every
- 7:58single developer has to recompile them
- 7:59and read it and
- 8:00look app update app update app update
- 8:03because everybody has to resend that
- 8:05that's the static model because you bake
- 8:06those old libraries in and if there's a
- 8:08change in the library i got to
- 8:09recompile it and read down and share up
- 8:11there's an update version
- 8:120.001 because the new library i used
- 8:15wasn't
- 8:15my code didn't work but the library
- 8:16updated i need to get make sure my users
- 8:18see that update
- 8:19because it's more efficient fixes a
- 8:21security hole whatever while i have to
- 8:22now resend all those updates to
- 8:24everybody that's annoying
- 8:25that's the statically linked model but
- 8:28the advantage is it's self-contained
- 8:29that's great the disadvantages
- 8:33i mean the other model the other model
- 8:34is a dynamically linked model and the
- 8:36dynamically link says you know what
- 8:38let's how about we no what if not what
- 8:40if we don't
- 8:41statically link that that library into
- 8:43the executable what if we just have a
- 8:46shrinks down to a one liner saying like
- 8:48pound kind of a pound include
- 8:49include this library at run time so at
- 8:52run time it will dynamically link
- 8:54in the library and then work it out so
- 8:55that the the loader will then
- 8:57make it run and do the right thing so it
- 8:58puts the puts the onus on the loader to
- 9:00fix it all
- 9:01at runtime which is interesting this is
- 9:03called a dll often
- 9:04in windows system for dynamically linked
- 9:06library
- 9:07again what are the advantages of a
- 9:09dynamically linked library well
- 9:10my program if i have 20 let's say i have
- 9:1220 different um
- 9:1420 different demos from opengl opengl is
- 9:17a graphics library so you know here's
- 9:18this
- 9:19square is moving around and here's
- 9:20circles moving around here's this maze
- 9:21blah blah blah here's a 3d
- 9:23something or ray tracing okay all these
- 9:25things are showing and highlighting the
- 9:26opengl library
- 9:27all 20 demos well as opengl
- 9:31is changing opengl now has a new version
- 9:33it's much faster
- 9:35instantly if i download that new version
- 9:37of the library
- 9:38all 20 get a benefit versus having to
- 9:41now redownload the demos for all 20 and
- 9:42re or recompile them myself
- 9:44we can do that as well i could update
- 9:47one library now the pointer to them so
- 9:49that's actually quite
- 9:50interesting rather than a way having
- 9:51copies that are baked in
- 9:53i reference it there so that's kind of
- 9:54nice sending a program requires less
- 9:57time if i
- 9:58take all the libraries i used to bloat
- 9:59my executables smaller now the sudden is
- 10:01getting downloading programmers faster
- 10:02maybe
- 10:03i'm paying for data on my cell phone
- 10:04service well that's great it's a faster
- 10:06download love that
- 10:07also executing two programs requires
- 10:10less memory
- 10:11in a way if they share a library you can
- 10:12be clever about how you actually load
- 10:14them in so that you don't
- 10:15you can be referencing the library and
- 10:17have the library running here and all on
- 10:18the reference in the library which also
- 10:19kind of is running as well so that could
- 10:20be really interesting to pick out
- 10:21depending how it's done
- 10:22at runtime now the downside is
- 10:26at runtime there's less overhead um
- 10:28there's sorry there's time overhead to
- 10:29do that link so now when i'm running
- 10:31rather than go boop i got all the stuff
- 10:32here just go
- 10:33now i have to okay go grab oh pound
- 10:34include where is that okay what's over
- 10:36here oh there's an error i could have an
- 10:37error at runtime
- 10:38that's an issue but it also means that
- 10:40runtime i have to do do the work of
- 10:42fixing those links and doing that
- 10:43at runtime so the startup cost is a
- 10:45little bit higher run time again but
- 10:46there's some benefits you saw that
- 10:48if i have an upgrade i mentioned this
- 10:49before if i have an upgrade i can just
- 10:50upgrade the
- 10:51that lib xyz xyz could be opengl
- 10:55math anything you want libmath.a is our
- 10:57math library certainly
- 10:58you could say lib whatever that is
- 11:00upgrade that and then just plug that in
- 11:02and now all of a sudden
- 11:03everybody gets the benefit of that
- 11:04that's great but it also means that the
- 11:06executable isn't enough anymore it means
- 11:08if i somehow corrupt the math library or
- 11:10corrupt the opengl library and
- 11:12my program my 20 programs are using that
- 11:14none of them will run
- 11:15they ran yesterday they don't run today
- 11:17that's a little scary that
- 11:19i didn't touch it i didn't touch this
- 11:21executable my machine doesn't touch it
- 11:23no memory works bus system works
- 11:24everything works but it doesn't work
- 11:26anymore why
- 11:27because somehow my my kid corrupted my
- 11:29opengl library and now none of my demos
- 11:31work and now i'm on stage on
- 11:32tv and then my my demos work that's a
- 11:35problem you can't really trust your
- 11:36executables anymore to work
- 11:37and advance you don't touch them because
- 11:38your libraries could be touched or i
- 11:40download the upgrade and the upgrade
- 11:41kind of quit halfway through i didn't
- 11:43know that it looks like i got it but i
- 11:44actually got half a file so now it's
- 11:45corrupted none of those work
- 11:46so again you have some trade-off there
- 11:48from runtime you know it's stable i
- 11:51downloaded it it's big and bloated but i
- 11:52know it's going to work versus
- 11:53it's smaller tighter downloading is
- 11:55faster i get the free upgrades if i
- 11:57upgrade that it'll work and i don't just
- 11:58kind of recompile
- 11:59but i go to run it and it may not fail
- 12:02runtime so again there's that
- 12:03and at the bottom the bottom line says
- 12:05overall dynamic linking adds quite a bit
- 12:07of work complexity to the compiler
- 12:08linker and certainly os which is the
- 12:10loader but some people find that their
- 12:12benefits outweigh those costs so that's
- 12:14pretty cool
- 12:15so in summary the prevailing approach to
- 12:18dynamic linking is to link at the lowest
- 12:20level you don't link at the upper level
- 12:21let's let's link at the assembler level
- 12:23no we link at the machine code level i
- 12:24link at the actual executable
- 12:26that's going to run i make some room for
- 12:27it grab the library stuff it in there
- 12:29and run it like that
- 12:30so that's again what the loader has to
- 12:31do and it's a lot harder to do that but
- 12:33that's the idea you have this lowest
- 12:34level it's called linking at machine
- 12:36code level
- 12:36this isn't the only way to do it but
- 12:38that's the prevailing model of how they
- 12:39do linking
- 12:40dynamically linked code
- 12:43now we're up to the loader we'll see the
- 12:45next lecture
About this transcript
This page contains the full transcript of [CS61C FA20] Lecture 13.4 - Compilation, Assembly, Linking, Loading: Linker by CS 61C Departmental, generated from the public captions YouTube serves with the video. The transcript has 2,915 words across 459 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.