From d125ba5dc5dc453df5e8c0fc63d2723af9388c23 Mon Sep 17 00:00:00 2001 From: Plus_Liu Date: Wed, 30 Sep 2020 17:15:25 +0800 Subject: [PATCH] change bounds_checking_function to libboundscheck --- Makefile | 44 ++++++++++++++++++++++++++++++++++++++++++++ README.en.md | 22 +++++++--------------- README.md | 23 +++++++++-------------- 3 files changed, 60 insertions(+), 29 deletions(-) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..bdeee08 --- /dev/null +++ b/Makefile @@ -0,0 +1,44 @@ +PROJECT=libboundscheck.so + +CC?=gcc + +OPTION = -fPIC +OPTION += -fstack-protector-all +OPTION += -D_FORTIFY_SOURCE=2 -O2 +OPTION += -Wformat=2 -Wfloat-equal -Wshadow +OPTION += -Wconversion +OPTION += -Wformat-security +OPTION += -Wextra +OPTION += --param ssp-buffer-size=4 +OPTION += -Warray-bounds +OPTION += -Wpointer-arith +OPTION += -Wcast-qual +OPTION += -Wstrict-prototypes +OPTION += -Wmissing-prototypes +OPTION += -Wstrict-overflow=1 +OPTION += -Wstrict-aliasing=2 +OPTION += -Wswitch -Wswitch-default + +CFLAG = -Wall -DNDEBUG -O2 $(OPTION) + +SOURCES=$(wildcard src/*.c) + +OBJECTS=$(patsubst %.c,%.o,$(SOURCES)) + +.PHONY:clean + +CFLAG += -Iinclude +LD_FLAG = -fPIC -s -Wl,-z,relro,-z,now,-z,noexecstack -fstack-protector-all + +$(PROJECT): $(OBJECTS) + mkdir -p lib + $(CC) -shared -o lib/$@ $(patsubst %.o,obj/%.o,$(notdir $(OBJECTS))) $(LD_FLAG) + @echo "finish $(PROJECT)" + +.c.o: + @mkdir -p obj + $(CC) -c $< $(CFLAG) -o obj/$(patsubst %.c,%.o,$(notdir $<)) + +clean: + -rm -rf obj lib + @echo "clean up" diff --git a/README.en.md b/README.en.md index 8ce43d7..394f9d5 100644 --- a/README.en.md +++ b/README.en.md @@ -1,4 +1,4 @@ -# bounds_checking_function +# libboundscheck #### Description @@ -46,22 +46,14 @@ - gets_s -#### Building +#### Build -- compilation steps - -1. Add all the .c files under /src to the source code listing for the build script. - -2. In the build options, specify the header directory and the build options required for the project (for example, add --Ipath_to_include -fstack-protector-strong -fPIC -Wall -D_FORTIFY_SOURCE=2 -O2 in CFLAGS). - -3. Generate .o files for each .c file. - -4. Generate static or shared libraries for .o files according to project requirements. - -- compiling examples: ``` -gcc -o memcpy_s.o -c -Iinclude -fstack-protector-strong -fPIC -Wall -D_FORTIFY_SOURCE=2 -O2 src/memcpy_s.c +CC=gcc make ``` +The generated Dynamic library libboundscheck.so is stored in the newly created directory lib. +#### How to use +1. Copy the libboundscheck.so to the library file directory, for example: "/usr/local/lib/". +2. To use the libboundscheck, add the “-lboundscheck” parameters to the compiler, for example: “gcc -g -o test test.c -lboundscheck”. \ No newline at end of file diff --git a/README.md b/README.md index 2f19960..1e0b3df 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# bounds_checking_function +# libboundscheck #### 介绍 - 遵循C11 Annex K (Bounds-checking interfaces)的标准,选取并实现了常见的内存/字符串操作类的函数,如memcpy_s、strcpy_s等函数。 @@ -42,20 +42,15 @@ - vswscanf_s - gets_s - #### 构建方法 -- 编译步骤 - -1. 将src下的.c文件添加到构建脚本的源码清单中。 - -2. 在编译选项中指定头文件目录以及项目需要的编译选项(例如:在CFLAGS中添加 -Ipath_to_include -fstack-protector-strong -fPIC -Wall -D_FORTIFY_SOURCE=2 -O2)。 - -3. 为每个.c文件编译生成.o文件 。 +运行命令 +``` +make CC=gcc +``` +生成的动态库libboundscheck.so存放在新创建的lib目录下。 -4. 根据项目需要将.o文件生成静态库或共享库使用。 +#### 使用方法 +1. 将构建生成的动态库libboundscheck.so放到库文件目录下,例如:"/usr/local/lib/"。 -- 编译示例: -``` -gcc -o memcpy_s.o -c -Iinclude -fstack-protector-strong -fPIC -Wall -D_FORTIFY_SOURCE=2 -O2 src/memcpy_s.c -``` \ No newline at end of file +2. 为使用libboundscheck,编译程序时需增加编译参数"-lboundscheck",例如:"gcc -g -o test test.c -lboundscheck"。 \ No newline at end of file -- Gitee