Linux開發心得總結20 - 核心編程中的全域變數使用(EXPORT_SYMBOL())__html5

來源:互聯網
上載者:User

和我們通常寫程式不同,如果僅僅是全域變數,雖然編譯核心的時候能串連成功,但是串連之後再就沒有辦法使用這個變數了

而模組的載入是運行時的,它引用某個變數時,核心需要解析它,否則模組不能工作,EXPORT_SYMBOL 的定義如下

fine __EXPORT_SYMBOL(sym, sec)                                /

        extern typeof(sym) sym;                                        /

        __CRC_SYMBOL(sym, sec)                                        /

        static const char __kstrtab_##sym[]                        /

        __attribute__((section("__ksymtab_strings")))                /

        = MODULE_SYMBOL_PREFIX #sym;                            /

        static const struct kernel_symbol __ksymtab_##sym        /

        __attribute_used__                                        /

        __attribute__((section("__ksymtab" sec), unused))        /

        = { (unsigned long)&sym, __kstrtab_##sym }

它放在固定的節,這樣核心解析的時候就能在這個節裡找符號了,找到了的重定位和普通的串連(.o) 原理類似////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

System.map 中的是連結時的函數地址。 串連完成以後,在核心運行過程中,是不知道哪個符號在那個地址的。而這個檔案是給調試用的。其中的內容,kernel並不知道 。


EXPORT_SYMBOL 的符號, 是把這些符號和對應的地址,儲存起來,在核心啟動並執行過程中,可以找到這些符號對應的地址的。

而module在載入過程中,其本質就是動態串連到核心,如果在模組中引用了核心或其它模組的符號,就要 EXPORT_SYMBOL 這些符號,這樣才能找到對應的地址串連呀。 要不沒法串連的。


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Linux symbol export methord [2007-03-06]

[1] If we want export the symbol in a module, just use the EXPORT_SYMBOL(xxxx) in the C or H file.
    And compile the module by adding the compile flag -DEXPORT_SYMTAB.
    Then we can use the xxxx in the other module.
[2] If we want export some symbol in Kernel that is not in a module such as xxxx in the /arch/ppc/fec.c.
    Firstly, define the xxxx in the fec.c;
    Secondly, make a new file which contain the "extern" define the xxxx(for example, extern int xxxx);
    Lastly, in the ppc_ksyms.c we includes the new file, and add the EXPORT_SYMBOL(xxxx).
    Then we can use the xxxx


insmod mod1.ko 必須先載入,因為函數體在他裡面,再載入insmod mod2.ko就正常

編譯選項中-DEXPORT_SYMTAB,同時要加上外部說明就少提示警告

下面簡單代碼:

/*mod1.c*/
#include  <linux/init.h>
#include  <linux/module.h>

MODULE_LICENSE("Dual BSD/GPL");

static int He1(void)
{
printk(KERN_INFO "He1..\n");
return 0;
}

EXPORT_SYMBOL(He1);

static int __init hello_init(void)
{
        printk(KERN_ALERT "Hello, world\n");
        return 0;
}

static void __exit hello_exit(void)
{
        printk(KERN_ALERT "Goodbye, cruel world\n");
}

module_init(hello_init);
module_exit(hello_exit);

==================================
/*mod2.c*/

#include  <linux/init.h>
#include  <linux/module.h>

MODULE_LICENSE("Dual BSD/GPL");

static int He2(void)
{
extern int He1(void);
He1();
printk(KERN_INFO "He2..\n");
return 0;
}


static int __init hello_init(void)
{
He2();
        printk(KERN_ALERT "Hello, world\n");
        return 0;
}

static void __exit hello_exit(void)
{
        printk(KERN_ALERT "Goodbye, cruel world\n");
}

module_init(hello_init);
module_exit(hello_exit);


### Makefile --- 

## Author: hefuhua@163.com
## Version: $Id: Makefile,v 0.0 2007/01/26 02:02:51 leno Exp $
## Keywords: 
## X-URL: 


ifneq ($(KERNELRELEASE),)
obj-m := -DEXPORT_SYMTAB #載入該參數的地方
obj-m := mod2.o
else

KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

endif

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

EXPORT_SYMBOL 使用小記

1.基本概念

EXPORT_SYMBOL 的用法就是將 一個函數 以符號的方式匯出給其他模組使用。

2.使用時注意事項:

在使用EXPORT_SYMBOL 的.c檔案中 需要 #include <linux/module.h> 檔案。

// 先寫函數

func_a ()

{

}

//再使用EXPORT_SYMBOL

EXPORT_SYMBOL(func_a);

  System.map與EXPORT_SYMBOL(待完善)2007-03-03 16:07System.map[1]是Linux核心符號檔案,維護有核心功能名稱和非堆棧變數名稱與各自地址的對應關係。

若核心功能或變數要被核心模組調用,則必須使用EXPORT_SYMBOL宏進行處理,作用之一是將該符號串連到二進位檔案的各個__ksymtab_xx_xx section(參看include/linux/module.h,使用GCC編譯器的__attribute__關鍵字實現[2])。核心載入模組時,會先確認該模組調用的各核心功能是否已export(參看__find_symbol() kernel/module.c)。

比如FC5預設會給vanilla核心打補丁,使其不再export sys_open符號,這一點可搜尋該核心對應的System.map檔案進行確認,看是否存在__ksymtab_sys_open符號。

[1] The system.map File
http://www.dirac.org/linux/system.map/

[2] Using GNU C __attribute__
http://www.unixwiz.net/techtips/gnu-c-attributes.html

 

  The system.map File
System.map
There seems to be a dearth of information about the System.map file. It's really nothing mysterious, and in the scheme of things, it's really not that important. But a lack of documentation makes it shady. It's like an earlobe; we all have one, but nobody really knows why. This is a little web page I cooked up that explains the why.

Note, I'm not out to be 100% correct. For instance, it's possible for a system to not have /proc filesystem support, but most systems do. I'm going to assume you "go with the flow" and have a fairly typical system.

Some of the stuff on oopses comes from Alessandro Rubini's "Linux Device Drivers" which is where I learned most of what I know about kernel programming.



What Are Symbols?
In the context of programming, a symbol is the building block of a program: it is a variable name or a function name. It should be of no surprise that the kernel has symbols, just like the programs you write. The difference is, of course, that the kernel is a very complicated piece of coding and has many, many global symbols.



What Is The Kernel Symbol Table?
The kernel doesn't use symbol names. It's much happier knowing a variable or function name by the variable or function's address. Rather than using size_t BytesRead(), the kernel prefers to refer to this variable as (for example) c0343f20.

Humans, on the other hand, do not appreciate addresses like c0343f20. We prefer to use something like size_t BytesRead(). Normally, this doesn't present much of a problem. The kernel is mainly written in C, so the compiler/linker allows us to use symbol names when we code and allows the kernel to use addresses when it runs. Everyone is happy.

There are situations, however, where we need to know the address of a symbol (or the symbol for an address). This is done by a symbol table, and is very similar to how gdb can give you the function name from an address (or an address from a function name). A symbol table is a listing of all symbols along with their address. Here is an example of a symbol table:

   c03441a0 B dmi_broken
   c03441a4 B is_sony_vaio_laptop
   c03441c0 b dmi_ident
   c0344200 b pci_bios_present
   c0344204 b pirq_table
   c0344208 b pirq_router
   c034420c b pirq_router_dev
   c0344220 b ascii_buffer
   c0344224 b ascii_buf_bytes
You can see that the variable named dmi_broken is at the kernel address c03441a0.



What Is The System.map File?
There are 2 files that are used as a symbol table:

/proc/ksyms
System.map
There. You now know what the System.map file is.

Every time you compile a new kernel, the addresses of various symbol names are bound to change.

/proc/ksyms is a "proc file" and is created on the fly when a kernel boots up. Actually, it's not really a file; it's simply a representation of kernel data which is given the illusion of being a disk file. If you don't believe me, try finding the filesize of /proc/ksyms. Therefore, it will always be correct for the kernel that is currently running.

However, System.map is an actual file on your filesystem. When you compile a new kernel, your old System.map has wrong symbol information. A new System.map is generated with each kernel compile and you need to replace the old copy with your new copy.



What Is An Oops?
What is the most common bug in your homebrewed programs? The segfault. Good ol' signal 11.

What is the most common bug in the Linux kernel? The segfault. Except here, the notion of a segfault is much more complicated and can be, as you can imagine, much more serious. When the kernel dereferences an invalid pointer, it's not called a segfault -- it's called an "oops". An oops indicates a kernel bug and should always be reported and fixed.

Note that an oops is not the same thing as a segfault. Your program cannot recover from a segfault. The kernel doesn't necessarily have to be in an unstable state when an oops occurs. The Linux kernel is very robust; the oops may just kill the current process and leave the rest of the kernel in a good, solid state.

An oops is not a kernel panic. In a panic, the kernel cannot continue; the system grinds to a halt and must be restarted. An oops may cause a panic if a vital part of the system is destroyed. An oops in a device driver, for example, will almost never cause a panic.

When an oops occurs, the system will print out information that is relevent to debugging the problem, like the contents of all the CPU registers, and the location of page descriptor tables. In particular, the contents of the EIP (instruction pointer) is printed. Like this:

   EIP: 0010:[<00000000>]
   Call Trace: [<c010b860>]


What Does An Oops Have To Do With System.map?
You can agree that the information given in EIP and Call Trace is not very informative. But more importantly, it's really not informative to a kernel developer either. Since a symbol doesn't have a fixed address, c010b860 can point anywhere.

To help us understand cryptic oops output, Linux uses a daemon called klogd, the kernel logging daemon. klogd intercepts kernel oopses and logs them with syslogd, changing some of the useless information like c010b860 with information that humans can use. In other words, klogd is a kernel message logger which can perform name-address resolution. Once klogd tranforms the kernel message, it uses whatever logger is in place to log system wide messages, usually syslogd.

To perform name-address resolution, klogd uses System.map. Now you know what an oops has to do with System.map.

There's other software besides the kernel logger daemon that uses System.map. I'll get into that shortly.

Fine print:
There are actually two types of address resolutions performed by klogd.

Static translation, which uses the System.map file.
Dynamic translation, which is used with loadable modules. These translations don't use System.map and is therefore not relevant to this discussion, but I'll describe it briefly anyhow:
Klogd Dynamic Translation
Suppose you load a kernel module which generates an oops. An oops message is generated, and klogd intercepts it. It is found that the oops occured at d00cf810. Since this address belongs to a dynamically loaded module, it has no entry in the System.map file. klogd will search for it, find nothing, and conclude that a loadable module must have generated the oops. klogd then queries the kernel for symbols that were exported by loadable modules. Even if the module author didn't export his symbols, at the very least, klogd will know what module generated the oops, which is better than knowing nothing about the oops at all.



Where Should System.map Be Located?
System.map should be located wherever the software that uses it looks for it. That being said, let me talk about where klogd looks for it. Upon bootup, if klogd isn't given the location of System.map as an argument, it will look for System.map in three places, in the following order:

/boot/System.map
/System.map
/usr/src/linux/System.map
System.map also has versioning information, and klogd intelligently searches for the correct map file. For instance, suppose you're running kernel 2.4.18 and the associated map file is /boot/System.map. You now compile a new kernel 2.5.1 in the tree /usr/src/linux. During the compiling process, the file /usr/src/linux/System.map is created. When you boot your new kernel, klogd will first look at /boot/System.map, determine it's not the correct map file for the booting kernel, then look at /usr/src/linux/System.map, determine that it is the correct map file for the booting kernel and start reading the symbols.

A few nota bene's:

Somewhere during the 2.5.x series, the Linux kernel started to untar into linux-version, rather than just linux (show of hands -- how many people have been waiting for this to happen?). I don't know if klogd has been modified to search in /usr/src/linux-version/System.map yet. TODO: Look at the klogd source. If someone beats me to it, please email me and let me know if klogd has been modified to look in the new directory name for the linux source code.
The man page doesn't tell the whole the story. Look at this:
      # strace -f /sbin/klogd | grep 'System.map'
      31208 open("/boot/System.map-2.4.18", O_RDONLY|O_LARGEFILE) = 2
Apparently, not only does klogd look for the correct version of the map in the 3 klogd search directories, but klogd also knows to look for the name "System.map" followed by "-kernelversion", like System.map-2.4.18. This is undocumented feature of klogd.

A few drivers need System.map to resolve symbols since they're linked against kernel headers instead of glibc). They won't work correctly without the System.map for the particular kernel currently running. This is NOT the same thing as a module not loading because of a kernel version mismatch. That has to do with the kernel version, not the kernel symbol table which changes between kernels of the same version!



What else uses the System.map
System.map isn't just useful for debugging kernel oopses. Other programs like lsof:

   satan# strace lsof 2>&1 1> /dev/null | grep System
   readlink("/proc/22711/fd/4", "/boot/System.map-2.4.18", 4095) = 23
and ps:

   satan# strace ps 2>&1 1> /dev/null | grep System
   open("/boot/System.map-2.4.18", O_RDONLY|O_NONBLOCK|O_NOCTTY) = 6
and dosemu require a correct System.map.



What Happens If I Don't Have A Healthy System.map?
Suppose you have multiple kernels on the same machine. You need a separate System.map file for each kernel! If you run a kernel with no (or an incorrect) System.map, you'll periodically see a message like:

System.map does not match actual kernel
Not a fatal error, but can be annoying to see everytime you use ps. Some software, like dosemu, may not work correctly. Lastly, your klogd or ksymoops output will not be reliable in case of a kernel oops.




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.