www.久久久久|狼友网站av天堂|精品国产无码a片|一级av色欲av|91在线播放视频|亚洲无码主播在线|国产精品草久在线|明星AV网站在线|污污内射久久一区|婷婷综合视频网站

當(dāng)前位置:首頁(yè) > > 充電吧
[導(dǎo)讀]How kernel, compiler, and C library work togetherKernel (內(nèi)核)The kernel is the core of an operating s

How kernel, compiler, and C library work together
Kernel (內(nèi)核)

The kernel is the core of an operating system. In a traditional design, it is responsible for memory management, I/O, interrupt handling, and various other things. And even while some modern designs like?Microkernels?or?Exokernels?move several of these services into user space, this matters little in the scope of this document.

The kernel makes its services available through a set of system calls; how they are called and what they do exactly differs from kernel to kernel.

內(nèi)核是操作系統(tǒng)的核心。在傳統(tǒng)的設(shè)計(jì)中,它是負(fù)責(zé)內(nèi)存管理,I / O,中斷處理,和其他各種東西。即使在一些現(xiàn)代的設(shè)計(jì),如微內(nèi)核或Exokernels這些服務(wù)的幾個(gè)移動(dòng)到用戶(hù)空間,這一點(diǎn)在本文的討論范圍的事項(xiàng)。
通過(guò)一組系統(tǒng)調(diào)用的內(nèi)核使得其提供的服務(wù),他們是如何完全不同。

C Library

One thing up front: When you begin working on your kernel, you do not have a C library available. You?must?not?#includeanything you did not write yourself. You will also have to port an existing C library. See?GCC Cross-Compiler, Future Steps / Standard Library.
第一件事:當(dāng)你開(kāi)始寫(xiě)你的內(nèi)核,如果你沒(méi)有一個(gè)C庫(kù)。你就不能#include 任何不是你自己寫(xiě)的東西。您可以移植現(xiàn)有的C庫(kù)。 GCC交叉編譯器,未來(lái)的步驟/標(biāo)準(zhǔn)庫(kù)。

The C library implements the standard C functions (i.e., the things declared in

C庫(kù)實(shí)現(xiàn)了標(biāo)準(zhǔn)的C函數(shù)(例如

In addition to standard C functions (as defined in the ISO standard), a C library might (and usually does) implement further functionality, which might or might not be defined by some standard. The standard C library says nothing about networking, for example. For Unix-like systems, the POSIX standard defines what is expected from a C library; other systems might differ fundamentally.

除了標(biāo)準(zhǔn)的C函數(shù)(如ISO標(biāo)準(zhǔn)中定義的),C庫(kù)(和通常一樣)可以實(shí)現(xiàn)更多的功能,這可能會(huì)超過(guò)一些標(biāo)準(zhǔn)。 在標(biāo)準(zhǔn)C庫(kù)裏,沒(méi)有關(guān)于網(wǎng)絡(luò)的function,例如。對(duì)于類(lèi)Unix系統(tǒng),POSIX標(biāo)準(zhǔn)定義C庫(kù)應(yīng)該的期望是什么。

It should be noted that, in order to implement its functionality, the C library must call kernel functions. So, for your own OS, you can of course take a ready-made C library and just recompile it for your OS - but that requires that you tell the library how to call your kernel functions, and your kernel to actually provide those functions. The good news is that?relatively few of the library's functions do use some system call

應(yīng)當(dāng)指出的是,為了實(shí)現(xiàn)其功能,C庫(kù)必須調(diào)用內(nèi)核函數(shù)。因此,對(duì)于自己的操作系統(tǒng),你當(dāng)然可以拿一個(gè)現(xiàn)成的C庫(kù),只需重新編譯它適用于您的操作系統(tǒng) - 但是這需要你告訴圖書(shū)館如何打電話(huà)給你的內(nèi)核函數(shù),和你的內(nèi)核,提供這些功能。好消息是,圖書(shū)館的功能比較少使用一些系統(tǒng)調(diào)用

Some implementations of the standard C library include:

GNU C library?(with info about?porting the glibc)newlib?(with info on the required OS functions detailed in the manual)uClibC?(although that is highly optimized to be used with an embedded?Linux).fdlibmdietlib

A more elaborate example is available in?Library Calls?or, you can roll your own?C Library.

Compiler / Assembler

An Assembler takes (plaintext) source code and turns it into (binary) machine code; more precisely, it turns the source into?object?code, which contains additional information like symbol names, relocation information etc.

一個(gè)assembler接受匯編源代碼為輸入,并把它轉(zhuǎn)化為機(jī)器代碼(二進(jìn)制);更確切地說(shuō),它原來(lái)的源代碼轉(zhuǎn)換成目標(biāo)代碼,其中包含其他信息,如符號(hào)名稱(chēng)(symbol),重定位信息等。

A compiler takes higher-level language source code, and either directly turns it into object code, or (as is the case with GCC) turns it into Assembler source code and invokes an Assembler for the final step.

編譯器接受更高層次的語(yǔ)言源代碼為輸入,可以直接把它轉(zhuǎn)換成目標(biāo)代碼,或像GCC那樣把它轉(zhuǎn)化為匯編程序源代碼,并調(diào)用匯編作為編譯的最后一步。

The resulting object code does?not?yet contain any code for standard functions called. If you?included e.g.??and usedprintf(), the object code will merely contain a?reference?stating that a function named?printf()?(and taking a?const char *?and a number of unnamed arguments as parameters) must be linked to the object code in order to receive a complete executable.

生成的目標(biāo)代碼不包含任何代碼的標(biāo)準(zhǔn)函數(shù)調(diào)用。如果包括了如

Some compilers use standard library functions?internally, which might result in object files referencing e.g.?memset()?or?memcpy()?even though you did not include the header or used a function of this name. You will have to provide an implementation of these functions to the linker, or the linking will fail (see below).

一些編譯器內(nèi)部使用標(biāo)準(zhǔn)庫(kù)函數(shù),這可能會(huì)導(dǎo)致在目標(biāo)文件中引用錯(cuò)誤,如memset的()的memcpy(),即使你沒(méi)有包含頭文件,或使用這個(gè)名字的函數(shù)。您必須提供一個(gè)實(shí)現(xiàn)這些功能的連接器,或否則會(huì)連接失敗(見(jiàn)下文)。

Some advanced operations (e.g. 64-bits divisions on a 32-bits system) might involve?compiler-internal?functions. For?GCC, those functions are residing in libgcc.a. The content of this library is agnostic of what OS you use, and it won't taint your compiled kernel with licensing issues of whatever sort, so you are welcome to locate "libgcc.a" and link it with your kernel.

一些先進(jìn)的操作(例如在32位系統(tǒng)的64位部門(mén))可能涉及部編譯器內(nèi)的功能。對(duì)于GCC,這些function是儲(chǔ)存在libgcc.a文件中。這個(gè)庫(kù)的內(nèi)容是用家不需要知的,所以編譯器總是希望你把“l(fā)ibgcc.a的”和將它連接到你的內(nèi)核編譯的內(nèi)核Link在一起.

Linker

A linker takes the object code generated by the compiler / assembler, and?links?it against the C library (and / or libgcc.a or whatever link library you provide). This can be done in two ways: static, and dynamic.

Static Linking

When linking statically, the linker is invoked during the build process, just after the compiler / assembler run. It takes the object code, checks it for unresolved references, and checks if it can resolve these references from the available libraries. It then adds the binary code from these libraries to the executable; after this process, the executable is?complete, i.e. when running it does not require anything but the kernel to be present.

On the downside, the executable can become quite large, and code from the libraries is duplicated over and over, both on disk and in memory.

Dynamic Linking

When linking dynamically, the linker is invoked during the?loading?of an executable. The unresolved references in the object code are resolved against the libraries currently present in the system. This makes the on-disk executable much smaller, and allows for in-memory space-saving strategies such as?shared libraries?(see below).

On the downside, the executable becomes dependent on the presence of the libraries it references; if a system does not have those libraries, the executable cannot run.

Shared Libraries

A popular strategy is to?share?dynamically linked libraries across multiple executables. This means that, instead of attaching the binary of the library to the executable image, the references in the executable are tweaked, so that all executables refer to the same in-memory representation of the required library.

This requires some trickery. For one, the library must either not have any?state?(static or global data) at all, or it must provide a separatestate?for each executable. This gets even trickier with multi-threaded systems, where one executable might have more than one simultaneous control flow.

Second, in a virtual memory environment, it is usually impossible to provide a library to all executables in the system at the same virtual memory address. To access library code at an arbitrary virtual address requires the library code to be?position independent?(which can be achieved e.g. by setting the -PIC command line option for the?GCC?compiler). This requires support of the feature by the binary format (relocation tables), and can result in slightly less efficient code on some architectures.

ABI - Application Binary Interface

The ABI of a system defines how library function calls and kernel system calls are actually done. This includes e.g. whether parameters are passed on the stack or in registers, how function entry points are located in libraries etc.

When using static linkage, the resulting executable is depending on the executing kernel using the same ABI as the one it was built for; when using dynamic linkage, the executable is depending on the libraries' ABI staying the same.

Unresolved Symbols

The linker is the stage where you will find out about stuff that has been added without your knowledge, and which is not provided by your environment. This can include references to?alloca(),?memcpy(), or several others. This is usually a sign that either your toolchain or your command line options are not correctly set up for compiling your own OS kernel - or that you are using functionality that is not yet implemented in your C library / runtime environment!

It is?strongly?recommended to build a?GCC Cross-Compiler?to avoid this and similar problems right from the start.

Other symbols, such as _udiv* or __builtin_saveregs, are available in the OS-agnostic "libgcc.a". If you get errors about missing such symbols, try to link your kernel against that library too. See?this thread?for details.

__alloca, ___main

alloca()?is a "semi-standard" C function (from BSD?, but supported by most C implementations) that is used to allocate memory from the stack. On Windows this function is also used for stack probing. As such,?alloca()?is referenced in PE binaries, build e.g. by the Cygwin GCC. You can set?-mno-stack-arg-probe?to suppress those references.

Another "specialty" of PE binaries is that, if you define?int main(), a function?void __main()?is called first thing after entering?main(). You can either define that function, or omit?main()?from your kernel code, using a different function as entry point.

This explanation of?alloca()?comes from Chris Giese, posted to alt.os.dev:

>> I think _alloca() is for stack-probing, which is required by Windows.
> What is stack probing?
By default, Windows reserves 1 meg of virtual memory for the stack. No page of stack memory is actually allocated (committed) until the page is accessed. This is demand-allocation. The page beyond the top of the stack is the guard page. If this page is accessed, memory will be allocated for it, and the guard page moved downward by 4K (one page). Thus, the stack can grow beyond the initial 1 meg. Windows will not, however, let you grow the stack by accessing discontiguous pages of memory. Going beyond the guard page causes an exception. Stack-probing code prevents this.

Some more information about stack-probing:

http://groups.google.com/groups?&selm=702bki%24oki%241%40news.Eindhoven.NL.nethttp://groups.google.com/groups?&selm=3381B63D.6E39%40austin.finnigan.com


alloca()?is not just used for stack-probing, but also as a sort of?malloc()?-- for dynamically allocating memory for variables/buffers -- without the need to manually free the reserved memory (with?free()?as it should be done for?malloc()?allocated memory).

If you search the man pages for alloca on a UNIX OS you'll find something like this: "The alloca() function allocates space in the stack frame of the caller, and returns a pointer to the allocated block. This temporary space is automatically freed when the function from which alloca() is called returns."


On Windows: "_alloca allocates size bytes from the program stack. The allocated space is automatically freed when the calling function exits (not when the allocation merely passes out of scope). Therefore, do not pass the pointer value returned by _alloca as an argument to free."

Note that "a stack overflow exception is generated if the space cannot be allocated", so?alloca()?might cause the stack to grow.

Unfortunately, there are all kinds of caveats with using?alloca(). Notably, Turbo~C/C++ had the limitation that functions calling?alloca()needed to have at least one local variable, and Linux man-page warns the following:

The alloca function is machine and compiler dependent. On many systems its implementation is buggy. Its use is discouraged.
On many systems alloca cannot be used inside the list of arguments of a function call, because the stack space reserved by alloca would appear on the stack in the middle of the space for the function arguments.

memcpy

This function is used internally by?GCC. You should set the?--no-builtin?switch, and provide your own implementation of?memcpy()?if you want to be independent of?GCC?(OSD library?just has it?;).


本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀(guān)點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請(qǐng)聯(lián)系該專(zhuān)欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除。
換一批
延伸閱讀

上篇文章中,小編對(duì)Turbo c編譯器已做部分介紹。本文中,小編將對(duì)Turbo c編譯器余下內(nèi)容加以講解。如果你對(duì)c編譯器具有興趣,不妨繼續(xù)往下閱讀哦。

關(guān)鍵字: c編譯器 turbo 指數(shù)

為增進(jìn)大家對(duì)c編譯器的認(rèn)識(shí),本文將對(duì)Turbo C c編譯器予以講解。如果你對(duì)c編譯器具有興趣,不妨繼續(xù)往下閱讀哦。

關(guān)鍵字: c編譯器 turbo 指數(shù)

本文中,小編將對(duì)c編譯器和c編輯器的區(qū)別予以介紹。如果你對(duì)c編譯器具有興趣,不妨繼續(xù)往下閱讀哦。

關(guān)鍵字: c編譯器 指數(shù) c編輯器

許多朋友經(jīng)常混淆c編譯器和c編輯器,注意,c編譯器的作用在于編譯,而c編輯器在于編寫(xiě)代碼。對(duì)于c編譯器,小編曾帶來(lái)諸多文章。但有網(wǎng)友提問(wèn),有沒(méi)有較為推薦的c編譯器。為解決大家的疑問(wèn),本文將對(duì)6款c編譯器予以介紹,大家可以...

關(guān)鍵字: c編譯器 指數(shù) 編譯器

c編譯器每天都在被使用,但對(duì)c編譯器十分了解的人卻不多,而對(duì)c編譯器編譯過(guò)程有所認(rèn)知的朋友更是少之又少。在上篇文章中,小編對(duì)c編譯器的工作過(guò)程有所講解。本文中,為繼續(xù)增進(jìn)大家對(duì)c編譯器的講解,將對(duì)編譯器工作過(guò)程的余下步驟...

關(guān)鍵字: c編譯器 指數(shù) 編譯器

c編譯器的問(wèn)世是程序界的里程碑之一,沒(méi)有c編譯器,c程序?qū)o(wú)法運(yùn)行。由此可見(jiàn),c編譯器尤為重要。為增進(jìn)大家對(duì)c編譯器的了解,本文將帶來(lái)c編譯器的通識(shí)篇,以使大家能夠了解編譯器的運(yùn)行過(guò)程。此外,本文僅為上篇,剩余內(nèi)容將在后...

關(guān)鍵字: c編譯器 指數(shù) 編譯器

對(duì)于c編譯器,很多朋友均有所了解。往期文章中,小編為大家?guī)?lái)過(guò)諸多c編譯器文章。在這篇c編譯器相關(guān)文章中,小編將介紹如何基于SDCC c編譯器開(kāi)發(fā)微控制器。如果你對(duì)c編譯器的應(yīng)用同樣存在興趣,不妨繼續(xù)往下閱讀哦。

關(guān)鍵字: c編譯器 sdcc 微控制器 指數(shù)

c編譯器尤為重要,缺乏c編譯器,很多應(yīng)用將無(wú)法運(yùn)行。此外,沒(méi)有c編譯器,很多系統(tǒng)同樣無(wú)法正常運(yùn)轉(zhuǎn)。在很多朋友眼里,對(duì)c編譯器充滿(mǎn)疑惑,如c編譯器是c語(yǔ)言編寫(xiě)的,那么第一個(gè)c編譯器是如何而來(lái)呢?如果你對(duì)這個(gè)問(wèn)題同樣不太了解...

關(guān)鍵字: c編譯器 C語(yǔ)言 匯編 指數(shù)

對(duì)于c編譯器,大家應(yīng)早已熟悉。往期文章中,小編帶來(lái)諸多c編譯器相關(guān)文章,尤其是gcc c編譯器。本文中,小編將對(duì)gcc c編譯器如何編譯c程序予以介紹,并在文章的后半部分向大家講解如果選擇pic單片機(jī)的c編譯器。如果你對(duì)...

關(guān)鍵字: c編譯器 gcc pic 指數(shù)

c編譯器的重要性不言而喻,從往期c編譯器文章中,如c編譯器優(yōu)化、選定c編譯器等,想必大家對(duì)c編譯器均已有所了解。往期文章中,小編主要從宏觀(guān)方面為大家講解c編譯器,此外對(duì)于gcc c編譯器的講解也大多基于windows。本...

關(guān)鍵字: c編譯器 gcc Linux
關(guān)閉