ã¯ããã«
æ¬è¨äºã¯Linuxç°å¢ã«ããã次ã®ãããªãã¨ããã£ããç解ããããã®è¨äºã§ãã
- Application Programming Interface(API)ã£ã¦ä½?
- Application Binary Interface(ABI)ã£ã¦ä½?
- ã·ã¹ãã ã³ã¼ã«ã¨APIã¨ABIã®é¢ä¿ã£ã¦ï¼
- ããããä½ãã©ãéãã®?
ãã®æã®æ å ±ã¯ã°ã°ãã°wikipediaããã«ããããæ å ±ãè¼ã£ã¦ããã§ãããåå¿è ãç解ããã«ã¯ç´°ãããã¨ã¾ã§æ¸ããããã¦ãã¦ããã¤ãããããã®é¢ä¿ããããã«ããã§ãããªã®ã§ãç¨èªãéä¸è§£èª¬ããã®ã§ã¯ãªãããããã¡ãªè³ªåã®QAã¨ããå½¢ãã¨ãã¾ããã人ã«ãã£ã¦ç¨èªã®æå³ã®æºããããã£ãããããã§ãããç§ã®è§£éã¨ãããã¨ã§ãããããã¾ã«ééã£ã¦ãããææãã¦ããã ããã¨å¬ããã§ãã
ãããæ¸ããã¨æã£ããã£ããã¯ã以åãããªtweetãè¦ããããã¨ã§ãããããããããããã°æè¿ä½¿ãããè¨èªã¯ã³ã¼ãããã¼ã¿ãã©ããããã¤ããªã«è½ã¡ãããè¦ããªããã®ãå¤ãã®ã§ããã®æã®ãã¨ã¯ããã¾ãç¥ããã¦ãªããªããªã¼ãç¥ã£ã¦ãã¨æ¥½ãããã ãã©ãªã¼ãã¨æã£ããã¨ã§ãã
APIã£ã¦ä½ï¼
APIã¯ãã½ã¼ã¹ã³ã¼ãã¬ãã«ã§é¢æ°(OOPLã®ã¡ã½ãããå«ã)ããã¼ã¿(OOPLã®ã¯ã©ã¹ãå«ã)ã®ä»æ§ãè¦å®ãããã®ã§ãããããã®é¢æ°ããã¼ã¿ãã©ã®ãããªãã¤ããªãã¼ã¿ã¨ãã¦è¡¨ç¾ããããã¯æ°ã«ãã¾ããã
ããæè¿ã§ã¯ããã°ã©ãã³ã°è¨èªã®APIã ãã§ã¯ãªããwebãµã¼ãã¹ã«ãªã¯ã¨ã¹ããåºãã¨ãã«ã©ãããURLã«ã©ããããªã¯ã¨ã¹ããåºãã¨ã©ãããã¬ã¹ãã³ã¹ãå¾ãããããè¦å®ããWeb APIã®ã»ãããªãã¿ãããããããã¾ããã
ã½ã¼ã¹ããã¨ã«APIã説æãã¾ãã以ä¸ã®ãããªCã½ã¼ã¹ãæ¸ãã¨ãã¾ãã
int plus(int x, int y) { return x + y; } int main(void) { int a = 1; int b = 2; .. plus(a, b): .. }
ãã®ã¨ãplus()ã®APIã¯ã第ä¸å¼æ°ã«intåã®å¤ã第äºå¼æ°ã«intåã®å¤ã渡ãã¨ãããã足ãåãããintåã®å¤ãè¿ããã§ããç¹°ãè¿ãã«ãªãã¾ãããCPUã®ã¢ã¼ããã¯ãã£ãCã³ã³ãã¤ã©ã®å®è£ ã«ä¾åããã¨ã³ãã£ã¢ã³ãintã®ãã¤ãæ°ãªã©ã¯æ°ã«ããªãã§ãã
ABIã£ã¦ä½ï¼
ABIã¯ãã¤ããªã¬ãã«ã§ãé¢æ°(OOPLã®ã¡ã½ãããå«ã)ããã¼ã¿(OOPLã®ã¯ã©ã¹ãå«ã)ã®ä»æ§ãè¦å®ãããã®ã§ããã¨ã³ãã£ã¢ã³ã¨ããã¼ã¿ã®ãµã¤ãºãæ°ã«ãã¾ãããã£ã¨ããã¨åç¯ã«è¨è¼ããã½ã¼ã¹ã§ããã¨plus()å¼ã³åºãæã«ã¢ã»ã³ããªè¨èªã¬ãã«ã§åå¼æ°ãã©ãããã¢ãã¬ã¹ã«é ç½®ãã¦ããã©ãããå½ä»¤ãå¼ã³åºããã¨ããå¼ã³åºãè¦ç´ãæ°ã«ãã¾ãã
å¼ã³åºãè¦ç´ã«ã¤ãã¦ã¯(ã¨ãã«x86_64ã¢ã¼ããã¯ãã£ã®)ã¢ã»ã³ããªè¨èªãè¥å¹²ã§ãç¥ã£ã¦ãã人ã¯ä»¥ä¸ã®æ å ±ãè¦ãã¨ãªãã¨ãªãããããã¨æãã¾ãã
- wikipediaã®"å¼ã³åºãè¦ç´"ã®ãã¼ã¸
- System V Application Binary Interfaceã®"3.2 Function Calling Sequence"
ABIã¯ãã¨ãã°æ¬¡ã®ãããªã¨ãã«å§¿ãç¾ãã¾ã
- ãã¤ããªå½¢å¼ã©ã¤ãã©ãªã®é¢æ°ãå¥ã®ã¢ããªãªããã©ã¤ãã©ãªããå¼ã³åºãã¨ã
- ããã°ã©ã ãã使ããããã³ã«ã®ãã¤ããªã¼ãã¼ãæå®ããã¦ããã¨ã
ãAPIãå©ããã£ã¦ã©ãããæå³ï¼
ãAPIã«å¾ã£ã¦é¢æ°ãå¼ã¶ããã¨ãæå³ããã¹ã©ã³ã°ã§ãããã¾ãæ·±ãèããªãã¦ãããã§ãã
POSIX APIã¨ã·ã¹ãã ã³ã¼ã«å¼ã³åºãã®éã
POSIX APIã¯POSIXã¨å¼ã°ããè¦æ ¼ã«ããã¦å®ç¾©ããã¦ããã主ã«UNIXç³»ã®OSéã§ç§»æ¤æ§ãé«ããããã«ä½ãããCè¨èªé¢æ°ã®APIã»ããã§ããlinuxã¯POSIXã«æºæ ãã¦ãã¾ããããPOSIXã«å®ç¾©ããã¦ããAPIã¯ããããåãã¦ãã¾ããUbuntuã«ããã¦ã¯manpages-posixãã¤ã³ã¹ãã¼ã«ãã¦man 3 read
ã¨ããã³ãã³ããå®è¡ããã¨POSIX APIã¨ãã¦ã®read()ã®ä»æ§ãèªãã¾ãã
ã·ã¹ãã ã³ã¼ã«ã¯ããã°ã©ã ãããã¼ãã¦ã§ã¢ãæä½ããããªã©ã®è¦æ±ãã«ã¼ãã«ã«ä¾é ¼ããæ¹æ³ã§ãããã¡ãã¯API(ãã¨ãã°man 2 read
ã¨ããã¨read()ã·ã¹ãã ã³ã¼ã«ã®å®ç¾©ã調ã¹ããã)ã¨å
±ã«ABIã決ã¾ã£ã¦ãã¾ãã
- System V Application Binary Interfaceã®A.2.1 Calling Conventions
é常ã¯ã¬ã¸ã¹ã¿ãªããã¡ã¢ãªä¸ã®æå®ã®ä½ç½®ã«å¼æ°ã®å¤ãæ¸ãè¾¼ãã§ããç¹æ®ãªã¢ã»ã³ããªå½ä»¤ãå¼ã³åºããã¨ããæ¹æ³ã§å¼ã³ã¾ã1ã
POSIX APIã®ãã¡ã®ã«ã¼ãã«ãå¼ã³åºãå¿ è¦ãããé¢æ°ã«ã¤ãã¦ã¯ABIã«åºã¥ãã¦ã·ã¹ãã ã³ã¼ã«ãå¼ã³åºãå¿ è¦ãããã¾ãããé¢æ°åã¨ã·ã¹ãã ã³ã¼ã«ã®ååãä¸å¯¾ä¸å¯¾å¿ãã¦ããå¿ è¦ã¯ããã¾ããããã¨ãã°glibcã«ããã¦ã¯fork()é¢æ°ãå¼ã³åºãã¨å é¨çã«ã¯clone()ã·ã¹ãã ã³ã¼ã«ãå¼ã³åºãã¾ããPOSIX APIã¨ãã¦fork()ã®ä»æ§ã«æ¸ãã¦ãããã¨ãå®ç¾ã§ããããå é¨çã«ã©ã®ã·ã¹ãã ã³ã¼ã«ã使ããã¨ããå®è£ 詳細ã¯ãªãã§ãããã®ã§ãã
ã·ã¹ãã ã³ã¼ã«ã¯Cè¨èªããããç´æ¥å¼ã³åºããªãã®ï¼
ãç´æ¥ãã®å®ç¾©ã«ãããã¾ãããå®ã¯ç§ã®æè¦ã¯ãã¢ã»ã³ããªè¨èªä»¥å¤ã®Cè¨èªãå«ãããããè¨èªã¯ç´æ¥ã·ã¹ãã ã³ã¼ã«ãå¼ã³åºããªããã§ãã
Cè¨èªã«ããã¦ãã·ã¹ãã ã³ã¼ã«ãå¼ã³åºãã¨ãã¯å é¨çã«ã¯ã¢ã»ã³ããªè¨èªã使ã£ã¦ãã¾ããä¾ãã°glibcã«ããã¦ã¯ããã°ã©ããglibcã®read()ãå¼ã³åºãã¨ããã®é¢æ°ã®ä¸ã§ã¯ã¤ã³ã©ã¤ã³ã¢ã»ã³ãã©ã¨ããæ©è½ã使ã£ã¦ã¢ã»ã³ããªè¨èªã«ãã£ã¦ã·ã¹ãã ã³ã¼ã«ãå¼ã³åºãã¦ãã¾ãããããã¯ã¢ã»ã³ããªè¨èªã§æ¸ãããã·ã¹ãã ã³ã¼ã«ãå¼ã³åºãé¢æ°ãå«ãã ã©ã¤ãã©ãªãå¼ã³åºãã¨ããæãããã¾ããããããæ¹æ³ããç´æ¥ãå¼ã³åºãã¦ããã¨æããã©ããã¯äººã«ããã§ãããããä¸è¿°ã®éããããã¯ããæãã¾ããã
Cè¨èªä»¥å¤ã®ãç´æ¥ã·ã¹ãã ã³ã¼ã«ãå¼ã³åºãããã¨è¨ããã¦ããè¨èªã«ããã¦ãæçµçã«ã¯ä¸è¨ã¨ä¼¼ããããªæ¹æ³ã使ã£ã¦ãã¾ãã
ããããã°ã©ãã³ã°è¨èªããä»ã®ããã°ã©ãã³ã°è¨èªã®é¢æ°ã¯ã©ããã£ã¦å¼ã³åºãã®ï¼
å¼ã³åºãå ã®è¨èªã¨å¼ã³åºãå ã®è¨èªã®éãç¹ããªãããã®ä»çµã¿ã使ãã¾ãããã¨ãã°Cã¨pythonãã¤ãªãæ¹æ³ã¯æ¬¡ã®éãã
ãªãã§ä½ã¬ã¤ã¤ããã°ã©ãã³ã°ã ã¨Cããã使ãããã®?
ããã¯äººã«ãã£ã¦æ§ã ã§ããããç§ãæãçç±ã¯æ¬¡ã®éãã
- ããã°ã©ããã¡ã¢ãªãã»ã¼å¥ãåºãã®å½¢ã§æ±ãã(è£ãè¿ãã°ãæ±ããªãã¦ã¯ãããªãã)2
- ç°ãªãã³ã³ãã¤ã©ãç°ãªããã¼ã¸ã§ã³éã§ã®ABIäºææ§ãé«ã
- è¨èªä»æ§ãå°ãããããå¦çç³»ãæ¯è¼çä½ããããããã¤ãã¡ã¢ãªæ¶è²»éãå°ãã
- ä½ã¬ãã«ã®APIè¦æ ¼ã¯POSIX APIã®ããã«Cè¨èªç¨ã®ãã®ãå¤ã
- ããè¨èªããå¥è¨èªãå¼ã³åºãããã«Cãä¸ç¶ãããã¨ãå¤ã(åç¯ãåç §)
- ååã«æ¯ãã¦ãããå¨è¾ºãã¼ã«ããããããã
ãããã«
æ°ãåãã°æ¬¡ã®ãããªãã¨ãæ±ã£ãç¶ç·¨ãæ¸ãããããã¾ããã
- äºææ§ã¨ã¯ä½ã
- ã©ã¤ãã©ãªã®ãã¼ã¸ã§ãã³ã°
- ELF symbol versioning
-
ã¢ã»ã³ããªè¨èªã¬ãã«ã®å½ä»¤ã使ããªãã¦ãæå®ã®ã¡ã¢ãªã«æ¸ãè¾¼ãã°ã·ã¹ãã ã³ã¼ã«ãå¼ã³åºãããã¨ãããããªå®è£ ãèãããã¾ãããããã¯ç½®ãã¨ãã¾ãã↩
-
ãã®ãã¨ããCè¨èªã¯ãé«ç´ã¢ã»ã³ããªè¨èªãã¨å¼ã°ãããã¨ããã↩