sprintf関数を自作OS上で使えるようにしたいです。(実行環境は32bit)
sprintf関数は内部でシステムコールの呼び出しがされていないから
OSの環境依存はなく自作OS上で呼び出すこともできるようです。
前提として・・・
ソースコードをsample.c
gcc -c -o sample.o sample.c
ld sample.o
objcopy -O binary a.exe sample
上記の手順でコンパイルします。(ぜったいに)
-cオプションを付けるとライブラリとリンクしてくれないようなので・・・
ライブラリのソースコードをそのまま貼り付けて使います。
sprintfのコードはこっちから入手しました。
sprintf関数の内部ではva_*系の関数も呼ばれているのでそちらも
コードに持ってきます。
"C:\MinGW\lib\gcc\mingw32\8.2.0\include\stdarg.h"
c
1/* Copyright (C) 1989-2018 Free Software Foundation, Inc. 2 3This file is part of GCC. 4 5GCC is free software; you can redistribute it and/or modify 6it under the terms of the GNU General Public License as published by 7the Free Software Foundation; either version 3, or (at your option) 8any later version. 9 10GCC is distributed in the hope that it will be useful, 11but WITHOUT ANY WARRANTY; without even the implied warranty of 12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13GNU General Public License for more details. 14 15Under Section 7 of GPL version 3, you are granted additional 16permissions described in the GCC Runtime Library Exception, version 173.1, as published by the Free Software Foundation. 18 19You should have received a copy of the GNU General Public License and 20a copy of the GCC Runtime Library Exception along with this program; 21see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 22<http://www.gnu.org/licenses/>. */ 23 24/* 25 * ISO C Standard: 7.15 Variable arguments <stdarg.h> 26 */ 27 28#ifndef _STDARG_H 29#ifndef _ANSI_STDARG_H_ 30#ifndef __need___va_list 31#define _STDARG_H 32#define _ANSI_STDARG_H_ 33#endif /* not __need___va_list */ 34#undef __need___va_list 35 36/* Define __gnuc_va_list. */ 37 38#ifndef __GNUC_VA_LIST 39#define __GNUC_VA_LIST 40typedef __builtin_va_list __gnuc_va_list; 41#endif 42 43/* Define the standard macros for the user, 44 if this invocation was from the user program. */ 45#ifdef _STDARG_H 46 47#define va_start(v,l) __builtin_va_start(v,l) 48#define va_end(v) __builtin_va_end(v) 49#define va_arg(v,l) __builtin_va_arg(v,l) 50#if !defined(__STRICT_ANSI__) || __STDC_VERSION__ + 0 >= 199900L \ 51 || __cplusplus + 0 >= 201103L 52#define va_copy(d,s) __builtin_va_copy(d,s) 53#endif 54#define __va_copy(d,s) __builtin_va_copy(d,s) 55 56/* Define va_list, if desired, from __gnuc_va_list. */ 57/* We deliberately do not define va_list when called from 58 stdio.h, because ANSI C says that stdio.h is not supposed to define 59 va_list. stdio.h needs to have access to that data type, 60 but must not use that name. It should use the name __gnuc_va_list, 61 which is safe because it is reserved for the implementation. */ 62 63#ifdef _BSD_VA_LIST 64#undef _BSD_VA_LIST 65#endif 66 67#if defined(__svr4__) || (defined(_SCO_DS) && !defined(__VA_LIST)) 68/* SVR4.2 uses _VA_LIST for an internal alias for va_list, 69 so we must avoid testing it and setting it here. 70 SVR4 uses _VA_LIST as a flag in stdarg.h, but we should 71 have no conflict with that. */ 72#ifndef _VA_LIST_ 73#define _VA_LIST_ 74#ifdef __i860__ 75#ifndef _VA_LIST 76#define _VA_LIST va_list 77#endif 78#endif /* __i860__ */ 79typedef __gnuc_va_list va_list; 80#ifdef _SCO_DS 81#define __VA_LIST 82#endif 83#endif /* _VA_LIST_ */ 84#else /* not __svr4__ || _SCO_DS */ 85 86/* The macro _VA_LIST_ is the same thing used by this file in Ultrix. 87 But on BSD NET2 we must not test or define or undef it. 88 (Note that the comments in NET 2's ansi.h 89 are incorrect for _VA_LIST_--see stdio.h!) */ 90#if !defined (_VA_LIST_) || defined (__BSD_NET2__) || defined (____386BSD____) || defined (__bsdi__) || defined (__sequent__) || defined (__FreeBSD__) || defined(WINNT) 91/* The macro _VA_LIST_DEFINED is used in Windows NT 3.5 */ 92#ifndef _VA_LIST_DEFINED 93/* The macro _VA_LIST is used in SCO Unix 3.2. */ 94#ifndef _VA_LIST 95/* The macro _VA_LIST_T_H is used in the Bull dpx2 */ 96#ifndef _VA_LIST_T_H 97/* The macro __va_list__ is used by BeOS. */ 98#ifndef __va_list__ 99typedef __gnuc_va_list va_list; 100#endif /* not __va_list__ */ 101#endif /* not _VA_LIST_T_H */ 102#endif /* not _VA_LIST */ 103#endif /* not _VA_LIST_DEFINED */ 104#if !(defined (__BSD_NET2__) || defined (____386BSD____) || defined (__bsdi__) || defined (__sequent__) || defined (__FreeBSD__)) 105#define _VA_LIST_ 106#endif 107#ifndef _VA_LIST 108#define _VA_LIST 109#endif 110#ifndef _VA_LIST_DEFINED 111#define _VA_LIST_DEFINED 112#endif 113#ifndef _VA_LIST_T_H 114#define _VA_LIST_T_H 115#endif 116#ifndef __va_list__ 117#define __va_list__ 118#endif 119 120#endif /* not _VA_LIST_, except on certain systems */ 121 122#endif /* not __svr4__ */ 123 124#endif /* _STDARG_H */ 125 126#endif /* not _ANSI_STDARG_H_ */ 127#endif /* not _STDARG_H */ 128
ということで・・・
・sprintfを呼び出したいカーネルのソースコード
・sprintf関数のソースコード
・stdarg.hのソースファイル
上記3つを合わせたものを実際にコンパイルしてみました。
c
1 int str1 = 999999999; 2 char str3[] = "000000000"; 3 protect_puts(str3,9,5,15); 4 sprintf(str3,"%d",str1); 5 protect_puts(str3,9,5,5); 6
成功すればstr3に999999999が入るので
000000000
999999999
となるはずですが
画面には
000000000
000000000
と表示されました。
つまり、sprintfが機能していません。
*<stdarg.h>のva_系関数は64bit専用?
c
1#define va_start(v,l) __builtin_va_start(v,l) 2#define va_end(v) __builtin_va_end(v) 3#define va_arg(v,l) __builtin_va_arg(v,l)
_builtinのラッパーを定義しています。
そのラッパー関数というのがva*関数(・・・?)
32bit環境では引数引き渡しにスタックが利用されてきましたが
64bitになってからスタックだけではなくレジスタも使われるようになり
ややこしくなったのでコンパイラの機能として__builtin系が用意された。
(私もよく知らない)
よって、自作OS上でsprintf関数が使えなかった理由は
sprintf内で呼ばれているva_*系関数が64bit対応のものであり
引数の引き渡し方が異なる
32bit環境では引数引き渡しに失敗して正常に動作しなかったのだと考えました。
上記のリンクを教えてもらいました。
スタック引き渡しを前提として書かれていたので32bitでもいける・・・?
・sprintfを呼び出したいカーネルのソースコード
・sprintf関数のソースコード
・上のサイトのva_*定義をそのまま持ってくる。
再度コードを組みコンパイルして実行してみましたが
結果は
000000000
000000000
sprintf関数がまたしても機能しません。
試しに64bit環境で動くか確認してみました・・・
(sprintfではなくsprintf2としているのは既に存在するsprintf関数と区別するため
関数名が被らないようにしました。)
c
1#include <stdio.h> 2 3#define va_list2 void* 4#define va_start2(void_p,argument) ((void_p)= &(argument) + sizeof(argument)) 5#define va_arg2(void_p,type) *((type*)(void_p+=sizeof(type)) - sizeof(type)) 6#define va_end2(void_p) ((ap) = (void_p)0) 7 8void main(void){ 9 int str1 = 999999999; 10 char str3[] = "000000000"; 11 printf("%s\n",str3); 12 sprintf2(str3,"%d",str1); 13 printf("%s\n",str3); 14} 15 16 17int dec2asc (char *str, int dec) { 18 int len = 0, len_buf; //桁数 19 int buf[10]; 20 while (1) { //10で割れた回数(つまり桁数)をlenに、各桁をbufに格納 21 buf[len++] = dec % 10; 22 if (dec < 10) break; 23 dec /= 10; 24 } 25 len_buf = len; 26 while (len) { 27 *(str++) = buf[--len] + 0x30; 28 } 29 return len_buf; 30} 31 32//16進数からASCIIコードに変換 33int hex2asc (char *str, int dec) { //10で割れた回数(つまり桁数)をlenに、各桁をbufに格納 34 int len = 0, len_buf; //桁数 35 int buf[10]; 36 while (1) { 37 buf[len++] = dec % 16; 38 if (dec < 16) break; 39 dec /= 16; 40 } 41 len_buf = len; 42 while (len) { 43 len --; 44 *(str++) = (buf[len]<10)?(buf[len] + 0x30):(buf[len] - 9 + 0x60); 45 } 46 return len_buf; 47} 48 49void sprintf2 (char *str, char *fmt, ...) { 50 va_list2 list; 51 int i, len; 52 va_start2 (list, fmt); 53 54 while (*fmt) { 55 if(*fmt=='%') { 56 fmt++; 57 switch(*fmt){ 58 case 'd': 59 len = dec2asc(str, va_arg2(list, int)); 60 break; 61 case 'x': 62 len = hex2asc(str, va_arg2 (list, int)); 63 break; 64 } 65 str += len; fmt++; 66 } else { 67 *(str++) = *(fmt++); 68 } 69 } 70// *str = 0x00; //最後にNULLを追加 71 //va_end (list); 72} 73
000000000
999999999
うまく変換できています。
64bitは引数をスタックだけではなくレジスタも使うはずですよね?
今回はたまたま引数引き渡しにレジスタが使われなかったから
うまく動作した・・・?
原因がさっぱりわかりません。
もしかしたらどこかで初歩的なミスをしているのかもしれません
分からないので教えてください。
追記
c
1void main_kernel(void){ 2 function(1,2,3,4,5); 3} 4 5void function(int a,...){ 6 7} 8
s
1.file "sample6.c" 2 .text 3 .globl _main_kernel 4 .def _main_kernel; .scl 2; .type 32; .endef 5_main_kernel: 6LFB0: 7 .cfi_startproc 8 pushl %ebp 9 .cfi_def_cfa_offset 8 10 .cfi_offset 5, -8 11 movl %esp, %ebp 12 .cfi_def_cfa_register 5 13 subl $40, %esp 14 movl $5, 16(%esp) 15 movl $4, 12(%esp) 16 movl $3, 8(%esp) 17 movl $2, 4(%esp) 18 movl $1, (%esp) 19 call _function 20 nop 21 leave 22 .cfi_restore 5 23 .cfi_def_cfa 4, 4 24 ret 25 .cfi_endproc 26LFE0: 27 .globl _function 28 .def _function; .scl 2; .type 32; .endef 29_function: 30LFB1: 31 .cfi_startproc 32 pushl %ebp 33 .cfi_def_cfa_offset 8 34 .cfi_offset 5, -8 35 movl %esp, %ebp 36 .cfi_def_cfa_register 5 37 nop 38 popl %ebp 39 .cfi_restore 5 40 .cfi_def_cfa 4, 4 41 ret 42 .cfi_endproc 43LFE1: 44 .ident "GCC: (MinGW.org GCC-8.2.0-3) 8.2.0" 45
引数の右からスタックに積まれています。
c
1#ifndef __SET_MACRO_VA_FUNCTION 2 3#define __SET_MACRO_VA_FUNCTION 4#define va_list2 void* 5#define va_start2(void_p,argument) ((void_p)= &(argument) + sizeof(argument)) 6#define va_arg2(void_p,type) *((type*)(void_p+=sizeof(type)) - sizeof(type)) 7#define va_end2(void_p) ((ap) = (void_p)0) 8 9#endif 10 11void main(void){ 12 function(1,2,3,4,5); 13} 14 15void function(int a,...){ 16va_list2 void_p; 17va_start2(void_p,a); 18printf("%p\n",void_p); 19va_arg2(void_p,int); //aの次 20printf("%p\n",void_p); 21 22} 23 24// 25// 26//==低位アドレス== 27// 28//a 29//b 30//c 31//d 32//e 33// 34// ===高位アドレス== 35// 36// 37
0061FF10
0061FF14

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/04/17 21:11
2020/04/17 21:55
2020/04/17 22:07