ºìÁªLinuxÃÅ»§
Linux°ïÖú

LinuxÄÚºËÑо¿ÏµÁÐÖ®¿ÉÖ´ÐÐÎļþ¸ñʽ

·¢²¼Ê±¼ä:2006-02-22 00:52:50À´Ô´:ºìÁª×÷Õß:´ó³¤½ñ
ÎÒÃÇÖªµÀlinux»·¾³Ï²»ÊÇËùÓеĶþ½øÖÆÎļþ¶¼ÓÐÏàͬµÄ¸ñʽ£¬linuxϵͳʹÓöþ½øÖÆÎļþµÄ´¦Àí³ÌÐòÀ´ÊµÏÖ¶Ô²»Í¬¶þ½øÖƸñʽÎļþµÄ·Ö±ð´¦Àí¡£¶þ½øÖÆ´¦Àí³ÌÐòͨ¹ýÄÚǶÔÚÎļþ¿ªÍ·µÄ¡°ÌØÕ÷ÐòÁС±£¨Ò»¸öÌØÊâµÄ×Ö½ÚÐòÁУ©À´Ê¶±ðÎļþ£¬ÓÐʱҲ»áͨ¹ýÎļþÃûµÄһЩÌØÕ÷£¬ÀýÈçELFÎļþÒÔ'E''L''F'×Ö·û¿ªÍ·£¬javaÎļþÒÔ0xcafebabe¿ªÊ¼Ç°Ëĸö×Ö½Ú¡£
LinuxÓÃsys_execve×°Èë¿ÉÖ´Ðжþ½øÖÆÎļþ¡£
µ±Ç°linux°æ±¾£¨2.2£©ÌṩÒÔϼ¸ÖÖ¶þ½øÖÆÎļþ´¦Àí³ÌÐò£º
l a.out:Ö÷ҪʱΪÁ˺ÍÒÔÇ°¼æÈÝ£¬ÒòΪa.outºÜÄÑʵÏÖ¶¯Ì¬Á´½Ó£¬ÒÔ±»ELFËùÈ¡¡£
Ïà¹ØÄÚÈݼû
l ELF£ºÏÖÔÚÖ÷Á÷µÄlinux¶þ½øÖÆÎļþ¡£¾¡¹ÜÈç´Ë£¬ËýÒ»ÑùÒªºÍÆäËû¸ñʽһÑùÐèÒª
ʹÓöþ½øÖÆ´¦Àí³ÌÐò¡£Ïà¹ØÄÚÈÝÔÚ
l EM86£ºÖ÷Òª×÷ÓÃÊÇÔÚAlphaµÄÖ÷»úÉÏÔËÐÐIntelµÄlinux¶þ½øÖÆÎļþ£¬·Â·ðËûÃǾÍÊÇAlphaµÄ±¾µØ¶þ½øÖÆÎļþ¡£Ïà¹ØÄÚÈÝ
l Java:ͨ¹ýÒÔ.classÎļþµÄÎļþÃûΪ²ÎÊý£¬´¦Àí³ÌÐò·µ»ØÖ´ÐÐ×Ö½ÚÂëµÄ½âÊͳÌÐò¡£Ïà¹ØÄÚÈݼû
l Misc:×îÃ÷ÖǵĶþ½øÖÆ´¦Àí³ÌÐòµÄ·½·¨¡£Ëýͨ¹ýÄÚǶµÄÌØÕ÷Êý×Ö»òÎļþÃûºó׺ʶ±ð¶þ½øÖƸñʽ¡£ÁíÍâËý¿ÉÒÔÔËÐÐÆÚÅäÖ㬶ø²»ÊÇÖ»ÄÜÔÚ±àÒëÆÚÅäÖã¬ÕâÑù£¬Äã¾Í¿ÉÒÔÔÚÔö¼Óжþ½øÖƸñʽÎļþµÄÖ§³Ö¶ø²»ÓÃÖØбàÒëÄںˡ£ÓÐ˵½«ÓÃËýÈ¡´úJavaºÍEM86¶þ½øÖÆ´¦Àí³ÌÐò¡£Ïêϸ²Î¼û
l ½Å±¾£ºÖ÷ÒªÖ§³Öshell½Å±¾¡¢Perl½Å±¾£¬¿íËÉÒ»µã˵£¬ËùÓÐÇ°ÃæÁ½¸ö×Ö·ûÊÇ£££¡µÄ¿ÉÖ´ÐÐÎļþ¶¼ÓÉËýÀ´´¦Àí¡£Ïêϸ²Î¼û
ÔÚ¼ÌÐøÏòϽéÉÜ֮ǰ£¬ÎÒÃDZØÐëÏÈÀ´ÈÏʶһ¸öÊý¾Ý½á¹¹linux_binfmt;

struct linux_binfmt{
struct linux_binfmt * next;
long *use_count;
int (*load_binary)(struct linux_binprm *,struct pt_regs *regs);
int (*load_shlib)(int fd);
int (*core_dump)(long signr,struct pt_regs *regs);
};
ÔÚlinux_binfmtÖаüº¬Á½¸öÖØÒªÖ¸Ïòº¯ÊýµÄÖ¸Õ룬load_binary×°Èë¿ÉÖ´ÐдúÂ룬load_shlib×°Èë¹²Ïí¿â¡£Core_dumpÊǸöת´¢º¯ÊýÖ¸Õë¡£
ºÜÏÔÈ»£¬ÓÉnext¹¹³ÉÒ»¸öÁ´±í£¬±íÍ·ÔòÓÉformatsÖ¸Ïò£¬ÈçÏÂͼ£º
static struct linux_binfmt *formats=(struct linux_binfmt *)NULL;
ϵͳΪÿ¸ö²»Í¬µÄÎļþ¸ñʽ¶¨ÒåÁËÒ»¸öÏàÓ¦µÄ¶ÔÏó:
static struct linux_binfmt elf_format;
static struct linux_binfmt java_format;
static struct linux_binfmt em86_format;
¡­¡­
linux_binfmtµÄÁ´±í(ÓÉformatsÖ¸Ïò)Ôò¾ÍÊÇÓÉÕâЩ²»Í¬µÄÎļþ¸ñʽµÄ
linux_binfmt¹¹³ÉµÄÒ»¸öÁ´±í¡£¶ø¶Ô²»Í¬¸ñʽ¶þ½øÖÆÎļþµÄ´¦Àí³ÌÐòÔòͨ¹ý×¢²áÔÚÏàÓ¦µÄlinux_binfmtÖеĺ¯ÊýÀ´ÊµÐУ¬Êµ¼ÊÕâÀïÒѾ­Ê¹ÓÃÁËÃæÏò¶ÔÏóµÄ˼Ï룬¶øÕâÖÖÇé¿öÔÚlinuxµÄÄÚºËÔ´ÂëÖо­³£¿ÉÒÔ¿´µ½¡£
ÀýÈç¶ÔÓÚELFÎļþ¸ñʽ£º
µ±Ê¹Óá°×°È뺯ÊýµÄÖ¸Õ롱ʹ£¬Ö¸ÕëÖ¸Ïò×°È뺯Êý·Ö±ðΪ£º
load_elf_binary(struct linux_binprm *bprm,struct pt_regs *regs);
static int load_elf_library(int fd);
ÒÔÉϺ¯ÊýÔÚ Öж¨ÒåʵÏÖ¡£
¶øÏàÓ¦µÄelf_format ±»¶¨Òå³É£º
static struct linux_bmfmt elf_format={
#ifndef MODULE
NULL,NULL,load_elf_binary,load_elf_library,elf_core_dump
#else
NULL,&mod_use_count,load_elf_binary,load_elf_library,
elf_core_dump()
#endif
};

ÒÔ´ËΪ²Î¿¼ÎÒÃÇ×ÔȻҲ¾Í¿ÉÒÔÏëµ½ÆäËûÎļþ¸ñʽµÄ¶¨ÒåÐÎʽ¡£
¡º×¢¡»JavaµÄloadº¯ÊýΪload_java,em86µÄloadº¯ÊýΪload_em86,script µÄloadº¯ÊýΪload_script,ËûÃÇûÓÐÌṩlibraryµÄloadº¯Êý¡£

ÏÂÃæÎÒÃÇÀ´¿´¿´¹ØÓÚÕ⼸¸öÊý¾Ý½á¹¹µÄÖ÷Òª²Ù×÷£º
l register_binfmt£ºÖ÷Òª×÷ÓÃʹ½«Ò»Öжþ½øÖÆÎļþ¼Óµ½formatsÁ´±íÖС£
extern int register_binfmt(struct linux_binfmt *);
l unregister_binfmt:Ö´ÐÐÓëregister_binfmtµÄ·´²Ù×÷¡£
extern int unregister_binfmt(struct linux_binfmt *);
l init£º¶ÔÓÚÿ¸ö²»Í¬µÄ¶þ½øÖÆÎļþ¸ñʽϵͳ¶¼ÌṩÁËÒ»×éinit²Ù×÷£¬ÈçÏ£º
extern int init_elf_binfmt(void);
extern int init_aout_binfmt(void);
extern int init_script_binfmt(void);
extern int init_java_binfmt(void);
extern int init_em86_binfmt(void);
extern int init_misc_binfmt(void);
ÒÔÉϺ¯ÊýÔÚ ÖÐÉùÃ÷£»
initº¯ÊýµÄ×÷Óúܼòµ¥£¬Ö»Êǵ÷ÓÃregister_binfmtº¯Êý½«×Ô¼ºµÄÎļþ¸ñʽ¼ÓÈëformatsÁ´±í¡£
l loadº¯Êý£ºÖ´Ðжþ½øÖÆÎļþµ÷È빤×÷£¬ÆäʵÖʹ¤×÷Ò»°ã¶¼ÓÉÒ»¸öÏàÓ¦do_loadº¯ÊýÀ´Íê³ÉÈ磺do_load_elf_binary,do_load_elf_library,do_load_java¡­¡­..
l extern int prepare_binprm(struct linux_binprm *);
extern int search_binary_handler(struct linux_binprm*,struct pt_regs *);
ÕâÁ½¸öº¯ÊýÖ÷Òª¾ÍÉæ¼°µ½ÁíÒ»¸öÊý¾Ý½á¹¹linux_binprm,ËýÂðÄǾÍÏ´ÎÔÙ˵°É£¬µ½ÕâÀï´ó¼ÒÓ¦¸Ã¶Ôlinuxϼ¸ÖÖ¶þ½øÖÆÎļþ´¦Àí³ÌÐòÓиö´óÌåµÄÓ¡ÏóÁË£¬¾ßÌåϸ½ÚÈ¥¿´¿´Ô´´úÂë°É¡£
ºÃÁË£¬Õâ´Î¾Íµ½ÕâÀïÁË£¬¸ÃÐÝÏ¢ÁË
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 0 ÌõÆÀÂÛ