What Happens to a Function After Entering the Compiler

Date: Oct 28, 2022

Related Tags:1. Function Compute
2. FAQ about Function Compute

Abstract: This article describes the experience of a function after entering the compiler.

i am a function

I'm a function, called str_upper, and I can change the input string from lowercase to uppercase. If you don't believe me, I look like this:

The above is the form of my source code. I heard my good friend str_lower say that we will be sent to a place called a compiler for processing together, and I am very scared.

Compiler Tour

It didn't take long for us to come here, a huge and high-rise building with many sophisticated machines running non-stop.

As soon as you enter the lobby, a lot of function code is waiting in line here.

I looked up and I didn't know how many floors there were. Each floor had a sign, which read from bottom to top:


lexical analysis

Gramma analysis

Semantic Analysis


Go up too far to see clearly.

All function codes are queued up in units of files and wait silently.

But it didn't take long before it was our team's turn.

A staff member came and took us to a room, let us all lie down, a machine quickly scanned it from beginning to end, and replaced all #include and #define that appeared in the file we were in.

Then, through the elevator in the room, we were sent to the second floor.

In the next period of time, we did "physical examinations" on several floors, and each function was photographed by those CT-like machines.

After a while, I came to the compilation layer. There is a very strange machine in this layer. I saw that functions were sent in, and they all changed when they came out. Not only that, but the staff at the reception looked fierce and I was even more nervous.

function calling convention

The staff got my information, took a few glances, and asked, "What's your calling convention?"

I was a little confused and didn't quite understand what he meant, so I asked in a low voice, "Excuse me, what did you just ask?"

The staff got a little impatient and raised the volume, "I'm asking you what the calling convention is? The calling convention!"

Seeing that I was still at a loss, the staff directly put a mark on the calling convention column of my data: cdecl.

I was a little confused, but my colleague str_lower dragged me and said: "He is asking you about the calling convention of the function, that is, the way to call the function, how to pass parameters, who will restore the call stack, etc."

After he said it, I reflected, "What are the options for this calling convention?"

"There are generally three types:"

cdcel, the parameters are pushed into the stack from right to left, and the calling function is responsible for restoring the stack balance

stdcall, parameters are pushed onto the stack from right to left, and the called function is responsible for restoring stack balance

fastcall, parameters are passed through registers, and the registers are not enough to be passed on the stack

"He just saw that you didn't declare it explicitly, and he gave you the cdecl method by default," the little friend continued.

I nodded, it turns out that calling a function has so much to do!

Stack Canary

"Stop chatting, come in!", the staff urged me.

I'm about to go to that horrible machine.

"Oh, wait a minute," I was nervous when the staff stopped me again.

I looked back and the staff was waving me over.

"Hello, is there something wrong with my code?" I asked nervously, for fear that if there was an error, it would be typed back, and our entire file would be repatriated.

"No, I noticed that there is a local array in your function, and you need to add stack overflow protection to you," the staff said.

I looked at my code and it does have a local character array:

"What is stack overflow protection?", I asked in a low voice.

The staff ignored me and was busy adding stuff to my profile.

The little friend next to me dragged me over again and said: "The local variables and parameters defined in our function are stored in the thread stack. The thread must keep walking in different functions. After calling the function, in order to return to In the original place, the return address was also placed on the thread stack before the call. Like this, you can see if there is any problem: "

I took a closer look, "Oh, if I access my upper array out of bounds, I can modify the return address, which is dangerous!"

"It's very clever!"

"How can this be protected?" I asked.

"Look, before the function comes in, set a value between the local variable and the return address, and check it before the function returns. If the data in the stack is corrupted, you can find out by checking this value and throw an exception in advance! ", the friend explained patiently.

"Well, wouldn't that call me back and add these settings and inspection codes you said?", I continued to ask.

At this time, the staff heard our chat, "No, our compiler added it automatically, go ahead, it's already processed"

I glanced over and saw that a tag called Stack Canary had been added to my profile.

I walked carefully into the strange machine, and immediately lost consciousness. When I woke up, my body had changed and became a bunch of strange codes. Now I look like this:


After a while, all the function codes of our team were compiled, and everyone moved from the original .c file to a new home: a .o file, and I saw my friend str_lower again.

"Have we finished compiling, can we leave here?"

"Not yet, although the compilation is complete, there is still one step to link!"

After a while, the function code of the other files that came with us was also compiled, and we were sent to the top floor of the compiler building together with a bunch of .o files: the link layer.

There is also a huge machine on this layer, and a pipe is connected to the back of the machine, which leads to nowhere.

All the .o files of our batch went into this huge machine one by one, like a time tunnel, walking through it, I felt a huge pressure squeeze us together, and soon we lost again consciousness.

After waking up, I found that all the functions were combined into one file, which is an executable file, and my body changed again, becoming a segment of binary instructions, and now I look like this :

It was an unforgettable journey to finally leave the compiler, but I don't want to come again...  

Easter eggs

Did not expect fate to play a joke on me, my first run went wrong!

I'm going to be beaten back and remade again, and I'll go on this diabolical journey all over again.

Can you help me see, is there anything wrong with my code?

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00

phone Contact Us