Skip to playerSkip to main contentSkip to footer
  • 6/2/2025
Hey everyone, let?s talk linkers! In this video, we unpack the general idea of linking in programming?taking your source code, turning it into object files, and stitching them into an executable. We dive into virtual memory, how programs use offsets, and why jump instructions matter across modules. Using simple assembly and C++ examples, we?ll show how linkers lay out modules, handle labels, and make function calls work. Whether you?re a beginner or a coder curious about what happens under the hood, this is for you! Hit subscribe, scan the QR code for more tutorials, and drop a comment with your thoughts or video ideas. Let?s keep learning and have some fun with code! #Programming #Linker #VirtualMemory #CodingExplained

Introduction to Linking 00:00:00
Compilation and Object Files 00:00:05
Linker Overview 00:00:32
Virtual Memory Explanation 00:00:42
Program Memory Offsets 00:01:18
Jump Instructions in Modules 00:02:54
Module Address Space 00:03:38
Assembler and Label Jumps 00:04:02
Linker?s Role in Module Layout 00:07:15
Function Calls Across Modules 00:08:04
Marking Labels as Global 00:09:31
External Function Calls 00:10:23
Data in Executables 00:11:12
Conclusion and Call to Subscribe 00:12:08


Thanks for watching!

Find us on other social media here:
- https://www.NeuralLantern.com/social

Please help support us!

- Subscribing + Sharing on Social Media
- Leaving a comment or suggestion
- Subscribing to our Blog
- Watching the main "pinned" video of this channel for offers and extras

Transcript
00:00Hey there, I want to talk to you very quickly about the general idea of linking your programs in
00:06Previous videos. I've described exactly how to perform the linking stage when you're compiling a program
00:11So you write some source code you compile the source code into object files
00:16You know each file becomes one individual object file and then when we're done
00:20We take all the object files and we link them up into an executable program
00:25So I've talked about that before in previous videos
00:27But I just wanted to talk to you about the general idea of the linking stage in general like what is the linker?
00:32What is its job? What is it actually doing?
00:41So try to keep in mind for starters each program uses virtual memory it uses a virtual memory space
00:48So obviously on your computer you have real memory
00:52But there's lots of layers of abstraction
00:55Between the physical hardware and the program that's running for starters
01:00The operating system uses a paging system
01:03The memory stick the memory circuitry might be doing something else for error checking or just you know transport or whatever
01:10So so try and bear in mind that each program thinks
01:13That it's it thinks that itself is kind of the beginning of memory
01:18You could imagine it as each program thinks that its first memory location that it has assigned to it
01:23Is memory location zero?
01:25But that wouldn't necessarily be the case on the computer
01:28Your operating system probably would have come up with an offset. Let's say for example
01:34You know your computer decides to launch your program it creates a process it finds a chunk of memory that's available
01:40Across you know one or more pages of free memory
01:43And let's say for the sake of argument that the starting offset for the for the quote-unquote real for getting the other layers
01:49Layers of abstraction the real memory location of your program is going to be let's just say like a thousand
01:54I know that's really unrealistically too low
01:58But let's just say a thousand and then let's say that um
02:01Your program then thinks that it starts at memory location zero
02:04That means if your program wants to access memory location 100
02:08Then under the hood at the last moment right before you actually hit the RAM stick or hit the page file system
02:15A translation has to be performed
02:17So if your program is trying to hit memory location 100
02:20What actually happens is you know the general offset for the entire program gets added to memory location 100
02:28And then that tells the computer or the operating system
02:31That what you're actually trying to do in your program is access
02:34Memory location 1100 so keep in mind all of this memory is virtual
02:40And the modules themselves also need to know where to jump
02:45When I say where to jump what I mean is kind of uh
02:49Well suppose for the sake of argument you have a program
02:52That has some jump instructions or go to instructions
02:56Uh or like some
02:58Conditional branching instructions anything where
03:01Execution is not just going to go to the very next statement
03:04Execution is going to go somewhere else like maybe you have an if else block
03:07It's got to maybe jump sometimes down to the else part instead of the top if part
03:12And then when it's done executing it's going to jump until after the whole block
03:15You know whatever it may be
03:16There will be there will be lots of times in all of your programs where execution kind of has to jump around somewhere
03:22If the program is even a little bit complicated so
03:25That could be a jump instruction go to in a higher level language not assembly necessarily
03:29Um jump if not equal uh and so forth so if you imagine writing one module
03:36You could also imagine that the module has its own
03:39Virtual memory address space just imagine it for now
03:42And and maybe it starts at zero that's that's not what we're going to say
03:45But just like imagine the module only really knows about itself
03:48And so sometimes when you're jumping for example if we're going to jump uh
03:53When something is not equal to something else
03:55We'll jump to some label
04:00And uh in order to jump to that label the assembler needs to know you know
04:04Where is that label inside of your source code so pretend just for a moment?
04:07This is not like a real assembly program
04:09But just pretend for a moment that we have a text section here
04:12And maybe this is like I don't know the main label that gets jumped into from gcc
04:18and um
04:20So then we have like a label down here outside
04:23And uh we'll do stuff
04:26And then jump back to the original uh point so we probably have to do a label here
04:32Main finish this is this is bad form
04:34But i'm just doing it to show you what the uh what the jumping is doing here
04:38So if we jump
04:43Let's compare
04:45I don't know one with two or something let's compare rx to
04:49rdx and we'll just
04:52Move rx the value one and move
04:55rdx the value two so they're not actually equal
04:58And then we compare them and then we say all right jump if they're not equal
05:01to this label so definitely now we know
05:03Oh we are going to jump down to that label so execution is going to go directly down to here
05:08We'll do something and then eventually maybe we want to jump back so we'll say uh
05:14Maybe let's do it again let's do
05:17Let's do like a whole set of statements again
05:19We'll say jump
05:21If it is equal and we'll just like load those two registers
05:25So one and one and then compare them and then say jump if it is equal to
05:29uh main finish
05:32This is not a video about conditional branching or jumping i'm trying to make this as simple as I can but
05:36I hope you understand the point is just basically we're jumping around within the same module right
05:42So it's easy for the program to understand
05:45Where to jump if it's in the same module or I guess the the object file when we're assembling it
05:50It's easy to understand where to jump
05:52Because if we're at line eight here when we start to jump
05:56And then the actual label here
05:58Has its first instruction at line 17 well then it you know the amount of instructions to move is just the difference
06:05You know, so it's easy to say
06:07All right, if our virtual address is you know this
06:11When we jump and we're going to jump this much of a difference based on where the label is
06:15Then now we can just compute
06:17The new virtual address to jump or the absolute address or whatever you want to whatever you want to do on that particular run
06:23But how does it know where to jump in another module because when we assemble this module as an object file?
06:31The assembler is only running one pass on just this object file. It doesn't really know the addresses in the other modules
06:38It doesn't know their offsets of its labels. Even if it's a c++ or a c module
06:42It doesn't really know the offsets. It doesn't know where the functions start and end doesn't know anything because
06:46Right now if we assemble this one source code, it's only going to know about this one source code, right?
06:51And the same goes for the other modules if we compile a c++
06:56program to an object file
06:58Then maybe if we want to call a function or you know do a go to statement or something into one of these labels from this module
07:06The c++ program won't really know where it is. It won't know what address it's at. It won't know the offset, right?
07:11So that's the job of the linker the linker looks at all of these labels and all of these
07:16Conditional branching statements and jump statements and everything and it just decides
07:21For starters, it decides where each module is going to start in memory. So maybe it'll say
07:26maybe it'll say uh
07:28I don't know if your c++ module starts up here somewhere and then it'll say this assembly module
07:33Starts right after it and then another module right after that and it'll just kind of decide
07:38Where every module will start in the final executable that we're actually linking together
07:43Once it decides the layout then it knows the offset of all the different modules and therefore it can compute
07:49The additional offset that it would take to get to all the labels. So then at that point
07:54If we want to jump, let's say from your c++ module, we'll make a function call inside of
08:00You know some assembly module like the one we're looking at assuming it was written a little bit differently
08:04Main finish return. Hmm. I guess the way it's written down here
08:10Uh, it's returning to main if we just kind of execute it as we're looking at it
08:14But another module could could call main finish as a function and still get a return statement
08:20That would actually work even though it's probably bad design
08:22But um, so yeah, so imagine that your assembly
08:25Sorry, your c++ program wants to call a function called main finish inside of this module
08:30The linker having already laid out the modules and and and being aware of all their offsets
08:35It knows okay
08:36So, you know this assembly module that was underneath the c++ module
08:40So it'll just take that offset and add it to
08:43The offset of the label itself within the module and then also add that to the
08:48The virtual address or I guess the operating system will do the the virtual address, but you know the point is the linker
08:54Coordinates all the jumps and the conditional branches where the functions start
09:00Lays out all the modules
09:02Gets all the offsets and then starts replacing all of the conditional branches and jumps and things
09:08With the real offsets that make it actually doable
09:11Like let's jump to exactly this memory location in code exactly in quotes because we're still using virtual addressing
09:17That's the basic idea of the linker
09:22It knows how to jump between modules
09:24Once the linker discuss, okay, that's all I really wanted to say
09:28Don't forget if you want a label to be available to jump into or to call as a function
09:33From outside the module you got to mark it as global
09:35So if I actually wanted somebody to be able to call main finish as a function, I just have to mark it as global
09:41Same thing for some label
09:43If I say global some label then now at this point
09:47Another module could call this as a function or jump into it probably bad form to not put a return a return statement at this point
09:53Which means when i'm calling it up here instead of jump not equal to I probably have to add more complicated logic so I can
10:01Use a function call that gets jumped around if it is equal, but so i'm not going to go that far right now kind of complicated
10:07Just for this video
10:10And then also of course if you intend to jump somewhere else
10:14Remember that you have to mark it as external so let's say there's a
10:18Function in your other module your c++ module or your c module or your
10:22assembly module
10:24You just have to mark it as extern in the text section in the text segment like some other
10:29function somewhere
10:31else something like that and then once I do that now I could actually call it I could say
10:39call you know this
10:42Actually, that'll never get reached because the jump equal thing is at the bottom, but I could do this
10:47I could say call it if it was actually a function or I could say jump if it was just a jump point or I could say
10:54You know jump not equal to
10:58The other function instead of some label something like that so
11:01Lots of stuff you can do and now you kind of understand the idea of linking is taking all the modules
11:06Stuffing them inside of the final executable and also putting data inside of the final executable
11:12You know like if you're creating a string
11:14Variable in your globals area of your assembly program or hard coding any kind of string anywhere and in your c++
11:21Then it's got to be inside of the executable. Otherwise, it wouldn't be anywhere when we ran the program
11:26So yeah, it sticks the data in there. It sticks the
11:29segments in there and you know calculates the jumps and all that stuff
11:33um
11:34I just want to point out you could put strings in a configuration file it happens a lot
11:38But you know for the purposes of a vanilla c++ or assembly program you you probably are making strings directly in the program
11:45Anyway, so I think that's the gist of everything that I really wanted to say about what is the general idea of the linker
11:54Thanks for watching this video. I hope you learned a little bit and had some fun. I
11:59Will see you in the next video
12:01Hey everybody, thanks for watching this video again from the bottom of my heart. I really appreciate it
12:08I do hope you did learn something and have some fun
12:11If you could do me a please a small little favor
12:14could you please
12:15subscribe and follow this channel or these videos or whatever it is you do on the current social media website that you're looking at right now
12:23It would really mean the world to me and it'll help make more videos
12:26And grow this community so we'll be able to do more videos longer videos better videos
12:31Or just i'll be able to keep making videos in general. So please
12:34Do do me a kindness and uh and subscribe you know sometimes
12:38I'm sleeping in the middle of the night and I just wake up because I know somebody subscribed or followed
12:43It just wakes me up and I get filled with joy. That's exactly what happens every single time
12:47So you could do it as a nice favor to me or you could you could troll me if you want to just wake me up in the middle
12:51And I just subscribe and then i'll i'll just wake up. I promise that's what will happen
12:57Also, uh, if you look at the middle of the screen right now, you should see a qr code
13:01Which you can scan in order to go to the website, which I think is also named somewhere at the bottom of this video
13:06And it'll take you to my main website where you can just kind of like see
13:10all the videos I published and the services and tutorials and things that I offer and all that good stuff and
13:15uh
13:18If you have a suggestion for uh
13:20uh clarifications or errata or just future videos that you want to see please leave a comment or if you just want to say
13:26Hey, what's up? What's going on? You know, just send me a comment, whatever
13:30I also wake up for those in the middle of the night. I get I wake up in a cold sweat. I'm like
13:35It would really it really mean the world to me. I would really appreciate it. So
13:39again, thank you so much for watching this video and
13:42um
13:43Enjoy the cool music as as I fade into the darkness, which is coming for us all
14:12You
14:42So
15:12So
15:42I
15:44I
15:46I
15:48I
15:50I
15:52I
15:54I
15:56I
15:58I
16:00I
16:02I
16:04I
16:06I
16:08I
16:10I
16:22I
16:24I
16:26I
16:28I
16:30I
16:32I
16:34I
16:36I
16:38I
16:52I
16:54I
16:56I
16:58I
17:00I
17:02I

Recommended