Adding Patch to a Shared Library

In this section, you are going to learn

How to create patches ?

How to add patches to Shared Library ?

How to add patches using diff and patch commands ?

How to add or import patches using quilt ?

Let us answer few basic questions

What is a patch ?

What are the ways to generate and apply patch ?

What is use of diff command ?

What is use of patch command ?

What is the command to generate patch file ?

What is the command to apply the patches to source code ?

What is the command to remove the patches from source code ?

What is quilt ?

What quilt init does ?

What quilt new does ?

What quilt add does ?

What quilt refresh does ?

What quilt series does ?

What quilt push does ?

What is quilt pop does ?

What quilt top does ?

Let us now explore it in depth !

  • Step 1: Edit the file which needs to be modified.

  • Step 2: Generate a patch file using the diff command.

diff -u <original_file_name> <modified_file_name> > <patch_file_name>
  • Step 3: Now the patches to the file can be applied using patch command.

patch <orginal_file_name> < <patch_file_name>
  • Now a patch is created and the changes is applied to the file.

1#ifndef LIBMATHSQUARE_H
2#define LIBMATHSQUARE_H
3
4int square(int x);
5
6#endif
7
1#include "libmathsquare.h"
2
3int square(int x) {
4	return x * x;
5}
6
1#include "libmathsquare.h"
2#include <stdio.h>
3
4int square(int x) {
5	printf("patch applied to shared library\n");
6	return x * x;
7}
8
$ diff -u libmathsquare_original.c libmathsquare_modified.c > add_patch_to_shared_lib.diff
$ patch libmathsquare_original.c < add_patch_to_shared_lib.diff
patching file libmathsquare_original.c
 1--- libmathsquare_original.c	2024-01-02 15:40:09.467460416 +0530
 2+++ libmathsquare_modified.c	2024-01-02 15:39:18.636036723 +0530
 3 -1,6 +1,8
 4 #include "libmathsquare.h"
 5 #include <stdio.h>
 6 
 7 int square(int x) {
 8+	printf("patch applied to shared library\n");
 9 	return x * x;
10 }
11 
$ gcc -o libmathsquare.so -shared libmathsquare_original.c
$ gcc -o main main.c -L./ -lmathsquare
$ export LD_LIBRARY_PATH=$PWD:$LD_LIBRARY_PATH
$ ./main
patch applied to shared library
Square of 5 is: 25
  • Step 1: create a new quilt series using quilt init command.

$ quilt init
The quilt meta-data is now initialized.
  • Step 2: create a patch file using quilt new command.

$ quilt new add_hello_world_patch.patch
Patch patches/add_hello_world_patch.patch is now on top
  • Step 3: add the file which needs to be modified using quilt add command.

$ quilt add helloworld.c
File helloworld.c added to patch patches/add_hello_world_patch.patch
  • Step 4: edit the file and make the modifications needed.

$ vi helloworld.c
  • Step 5: once the changes is added run quilt refresh to update the series with latest modifications.

$ quilt refresh
Refreshed patch patches/add_hello_world_patch.patch
  • Now a patch is created and changes has been added to the series.

1#ifndef LIBMATHSQUARE_H
2#define LIBMATHSQUARE_H
3
4int square(int x);
5
6#endif
7
1#include "libmathsquare.h"
2#include <stdio.h>
3
4int square(int x) {
5	printf("patch applied to shared library\n");
6	return x * x;
7}
8
 1CFLAGS = -Wall -O2 -fPIC
 2LIBRARY_NAME = libmathsquare
 3SHARED_LIBRARY = $(LIBRARY_NAME).so
 4SOURCE = $(LIBRARY_NAME).c
 5HEADERS = $(LIBRARY_NAME).h
 6
 7PATCHES_DIR = patches
 8
 9all: apply_patches $(SHARED_LIBRARY)
10
11$(SHARED_LIBRARY): $(SOURCE)
12	gcc $(CFLAGS) -shared -o $(SHARED_LIBRARY) $(SOURCE)
13
14apply_patches:
15	quilt push || true
16
17.PHONY: clean
18clean:
19	quilt pop -a || true
20	rm -f $(SHARED_LIBRARY)
21
1#include <stdio.h>
2#include "libmathsquare.h"
3
4int main() {
5	int num = 5;
6	printf("Square of %d is: %d\n", num, square(num));
7	return 0;
8}
9
 1CFLAGS = -Wall -O2
 2EXECUTABLE = main
 3SOURCE = main.c
 4LIBRARY_NAME = libmathsquare
 5SHARED_LIBRARY = $(LIBRARY_NAME).so
 6
 7LIBRARY_DIR = ../lib
 8PATCHES_DIR = patches
 9
10all: apply_patches $(EXECUTABLE)
11
12$(EXECUTABLE): $(SOURCE) $(LIBRARY_DIR)/$(SHARED_LIBRARY)
13	gcc $(CFLAGS) -o $(EXECUTABLE) $(SOURCE) -L$(LIBRARY_DIR) -lmathsquare -I$(LIBRARY_DIR)
14
15apply_patches:
16	quilt push || true
17
18.PHONY: clean
19clean:
20	quilt pop -a || true
21	rm -f $(EXECUTABLE)
22
$ quilt init
The quilt meta-data is now initialized.
$ quilt new add_static_lib_patch.patch
Patch patches/add_static_lib_patch.patch is now on top
$ quilt add libmathsquare.c
File libmathsquare.c added to patch patches/add_static_lib_patch.patch
$ vi libmathsquare.c
$ quilt refresh
Refreshed patch patches/add_static_lib_patch.patch
 1Index: lib/libmathsquare.c
 2===================================================================
 3--- lib.orig/libmathsquare.c
 4+++ lib/libmathsquare.c
 5 #include "libmathsquare.h"
 6 #include <stdio.h>
 7 
 8 int square(int x) {
 9+	printf("patch applied to shared library\n");
10 	return x * x;
11 }
12 
$ make
Applying patch patches/add_shared_lib_patch.patch
patching file libmathsquare.c

Now at patch patches/add_shared_lib_patch.patch
gcc -Wall -O2 -fPIC -shared -o libmathsquare.so libmathsquare.c
$ make
No series file found
gcc -Wall -O2 -o main main.c -L../lib -lmathsquare -I../lib
$ export LD_LIBRARY_PATH=../lib/:$LD_LIBRARY_PATH
$ ./main
Patch added to static library
Square of 5 is: 25