How Linkers Work: Linking Code, Virtual Memory, and Module Jumps Explained

How Linkers Work: Linking Code, Virtual Memory, and Module Jumps Explained

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

Hey there, I want to talk to you very quickly about the general idea of linking your programs.

In previous videos I’ve described exactly how to perform the linking stage when you’re compiling

a program. So you write some source code, you compile the source code into object files,

you know each file becomes one individual object file, and then when we’re done we take all the

object files and we link them up into an executable program. So I’ve talked about that

I just wanted to talk to you about the general idea of the linking stage in general.

Like what is the linker? What is its job? What is it actually doing?

So try to keep in mind for starters, each program uses virtual memory.

It uses a virtual memory space.

So obviously on your computer you have real memory,

but there’s lots of layers of abstraction between the physical hardware

the physical hardware and the program that’s running for starters the operating system uses

a paging system the memory stick the memory circuitry might be doing something else for

error checking or just you know transport or whatever so so try and bear in mind that each

program thinks that it’s it thinks that itself is kind of the beginning of memory you could imagine

it as each program thinks that its first memory location that it has assigned to it is memory

location zero but that wouldn’t necessarily be the case on the computer

your operating system probably would have come up with an offset let’s say

for example you know your computer decides to launch your program it

creates a process it finds a chunk of memory that’s available across you know

one or more pages of free memory and let’s say for the sake of argument that

the starting offset for the for the quote-unquote real forgetting the other

layers layers of abstraction the real memory location of your program is going

of your program is going to be let’s just say like a thousand I know that’s

real unrealistically too low but let’s just say a thousand and then let’s say

that your program then thinks that it starts at memory location zero that

means if your program wants to access memory location 100 then under the hood

at the last moment right before you actually hit the RAM stick or hit the

page file system a translation has to be performed so if your program is trying

what actually happens is you know the general offset for the entire program

gets added to memory location 100 and then that tells the computer or the

operating system that what you’re actually trying to do in your program is

access memory location 1100 so keep in mind all of this memory is virtual

and the modules themselves also need to know where to jump when I say where to

Well, suppose for the sake of argument, you have a program that has some jump instructions

or go to instructions or like some conditional branching instructions, anything where execution

is not just going to go to the very next statement.

Execution is going to go somewhere else.

Like maybe you have an if else block.

It’s got to maybe jump sometimes down to the else part instead of the top if part.

And then when it’s done executing, it’s going to jump until after the whole block, you know,

There will be lots of times in all of your programs where execution kind of has to jump

around somewhere if the program is even a little bit complicated.

So that could be a jump instruction, go to in a higher level language, not assembly necessarily,

jump if not equal and so forth.

So if you imagine writing one module, you could also imagine that the module has its

own virtual memory address space.

Just imagine it for now.

And maybe it starts at zero.

at zero that’s that’s not what we’re going to say but just like imagine the module only really knows

about itself and so sometimes when you’re jumping for example if we’re going to jump uh when

something is not equal to something else we’ll jump to some label and in order to jump to that

label the assembler needs to know you know where is that label inside of your source code so pretend

just for a moment this is not like a real assembly program but just pretend for a moment that we have

and maybe this is like I don’t know the main label that gets jumped into from GCC and

so then we have like a label down here outside and we’ll do stuff and then jump back to the

original point so we probably have to do a label here main finish this is this is bad form but I’m

So if we jump, let’s compare, I don’t know, one with two or something.

Let’s compare RAX to RDX and we’ll just move RAX the value one and move RDX the value two

so they’re not actually equal.

And then we compare them and then we say, all right, jump if they’re not equal to this label.

So definitely now we know, oh, we are going to jump down to that label.

So execution is going to go directly down to here.

we’ll do something and then eventually maybe we want to jump back so we’ll say uh

maybe let’s do it again let’s do let’s do like a whole set of statements again we’ll say jump

if it is equal and we’ll just like load those two registers so one and one and then compare

them and then say jump if it is equal to main finish this is not a video about conditional

branching or jumping i’m trying to make this as simple as i can but i hope you understand the

but i hope you understand the point is just basically we’re jumping around within the same

module right so it’s easy for the program to understand where to jump if it’s in the same

module or i guess the the object file when we’re assembling it it’s easy to understand where to jump

because if we’re at line 8 here when we start to jump and then the actual label here has its first

instruction at line 17 well then it you know the amount of instructions to move is just the

the difference you know so it’s easy to say all right if our virtual address is you know this

when we jump and we’re going to jump this much of a difference based on where the label is then now

we can just compute the new virtual address to jump or the absolute address or whatever you want

to whatever you want to do on that particular run but how does it know where to jump in another

module because when we assemble this module as an object file the assembler is only running one pass

one pass on just this object file it doesn’t really know the addresses in the other modules

it doesn’t know their offsets of its labels even if it’s a c plus plus or a c module it doesn’t

really know the offsets it doesn’t know where the functions start and end doesn’t know anything

because right now if we assemble this one source code it’s only going to know about this one source

code right and the same goes for the other modules if we compile a c plus plus program to an object

a function or you know do a go to statement or something into one of these labels from this

module the c plus plus program won’t really know where it is it won’t know what address it’s at it

won’t know the offset right so that’s the job of the linker the linker looks at all of these labels

and all of these uh conditional branching statements and jump statements and everything

and it just decides for starters it decides where each module is going to start in memory so maybe

I don’t know if your C++ module starts up here somewhere and then it’ll say this assembly module

starts right after it and then another module right after that and it’ll just kind of decide

where every module will start in the final executable that we’re actually linking together.

Once it decides the layout then it knows the offset of all the different modules and therefore

it can compute the additional offset that it would take to get to all the labels. So then at that

Your C++ module will make a function call inside of, you know, some assembly module like the one we’re looking at, assuming it was written a little bit differently.

Main finish return.

I guess the way it’s written down here, it’s returning to main if we just kind of execute it as we’re looking at it.

But another module could could call main finish as a function and still get a return statement.

That would actually work, even though it’s probably bad design.

that your assembly sorry your C++ program wants to call a function called

main finish inside of this module the linker having already laid out the

modules and and being aware of all their offsets it knows okay so you know this

assembly module it was underneath the C++ module so it’ll just take that offset

and add it to the offset of the label itself within the module and then also

add that to the the virtual address or I guess the operating system will do the

address. But you know, the point is the linker coordinates all the jumps and the conditional

branches where the functions start, lays out all the modules, gets all the offsets,

and then starts replacing all of the conditional branches and jumps and things with the real

offsets that make it actually doable. Like let’s jump to exactly this memory location

in code exactly in quotes, because we’re still using virtual addressing.

idea of the linker it knows how to jump between modules um wants to link or discuss okay that’s

all i really wanted to say don’t forget if you want a label to be available to jump into or to

call as a function from outside the module you got to mark it as global so if i actually wanted

somebody to be able to call main finish as a function i just have to mark it as global same

thing for some label if i say global some label then now at this point another module could call

call this as a function or jump into it probably bad form to not put a return a return statement

at this point which 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 use 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 just for this video

and then also of course if you intend to jump somewhere else remember that you have to mark

So let’s say there’s a function in your other module, your C++ module or your C module or your assembly module.

You just have to mark it as extern in the text section, in the text segment, like some other function somewhere else, something like that.

And then once I do that, now I could actually call it, I could say, call, you know, this.

Actually, that’ll never get reached because the jump equal thing is at the bottom.

equal thing is at the bottom but I could do this I 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 you know jump not equal to

the other function instead of some label something like that so lots of stuff you can do and now you

kind of understand the idea of linking is taking all the modules stuffing them inside of the final

executable and also putting data inside of the final executable you know like if you’re creating

So, you know, like if you’re creating a string variable in your globals area of your assembly

program or hard coding, any kind of string anywhere in your C++, then it’s got to be

inside of the executable.

Otherwise, it wouldn’t be anywhere when we ran the program.

So, yeah, it sticks the data in there.

It sticks the segments in there.

It, you know, calculates the jumps and all that stuff.

I just want to point out you could put strings in a configuration file.

of a vanilla C++ or assembly program you probably are making strings directly in the program.

Anyway, so I think that’s the gist of everything that I really wanted to say about what is the

general idea of the linker. Thanks for watching this video I hope you learned a little bit and

had some fun. I will see you in the next video. Hey everybody thanks for watching this video again

it i do hope you did learn something and have some fun if you could do me a please a small little

favor could you please subscribe 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 it would really mean the

world to me and it’ll help make more videos and grow this community so we’ll be able to do more

videos longer videos better videos or just i’ll be able to keep making videos in general so please

and subscribe. You know, sometimes I’m sleeping in the middle of the night and I just wake up

because I know somebody subscribed or followed. It just wakes me up and I get filled with joy.

That’s exactly what happens every single time. So you could do it as a nice favor to me or you

could troll me if you want to just wake me up in the middle of the night, just subscribe

and then I’ll just wake up. I promise that’s what will happen. Also, if you look at the middle of

the screen right now, you should see a QR code, which you can scan in order to go to the website,

is also named somewhere at the bottom of this video and it’ll take you to my main website where

you can just kind of like see all the videos I published and the services and tutorials and

things that I offer and all that good stuff and uh if you have a suggestion for uh uh clarifications

or errata or just future videos that you want to see please leave a comment or if you just want to

say hey what’s up what’s going on you know just send me a comment whatever I also wake up for

I wake up in a cold sweat and I’m like, it would really mean the world to me.

I would really appreciate it.

So again, thank you so much for watching this video and enjoy the cool music as I fade into

the darkness, which is coming for us all.

Thank you.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply