Open-source News

Linux 5.19 Adding Support For The PolarBerry RISC-V FPGA Board

Phoronix - Sat, 06/04/2022 - 01:43
A few days ago the RISC-V pull request landed in Linux 5.19 with support for RISC-V 32-bit (RV32) binaries on RV64, enabling the new Svpbmt extension, and other improvements. On Friday a secondary set of RISC-V changes were sent in for Linux 5.19 that includes adding the DeviceTree files for another new RISC-V board...

Zstd Compressed Firmware Submitted For Linux 5.19, Other Driver Core Changes

Phoronix - Fri, 06/03/2022 - 21:50
Greg Kroah-Hartman has gotten onto submitting his feature pull requests to Linux 5.19 for various areas of the kernel he oversees. With the driver core changes with this new kernel is now the ability to compress firmware files using the popular Zstd...

SiFive RISC-V Sees Some Performance Improvements On Ubuntu 22.04

Phoronix - Fri, 06/03/2022 - 19:50
While SiFive has sadly shutdown production on the current HiFive Unmatched development board in order to focus on new products expected later this year, those with a SiFive HiFive Unmatched RISC-V developer board can enjoy seeing continued performance improvements with newer Linux distributions. Here is a look at the SiFive FU740 SoC performance under the recently released Ubuntu 22.04 LTS compared to the prior 21.10 and 20.04 releases.

NTFS3 Kernel Driver Sees Fixes Sent In For Linux 5.19

Phoronix - Fri, 06/03/2022 - 19:37
Following the recent concerns around maintenance for the NTFS3 kernel driver and other developers stepping up to maintain the "NTFS3" kernel driver contributed by Paragon Software, there is now a batch of fixes ready to go for Linux 5.19...

Ubuntu 22.10 Looking At Replacing WPA With IWD For Linux Wireless

Phoronix - Fri, 06/03/2022 - 18:08
Adding to the list of planned changes for the Ubuntu 22.10 release this October is transitioning from WPA_Supplicant to Intel's IWD daemon for Linux wireless needs...

AMD Preparing To Finally Support Virtual NMI With Their CPUs (VNMI)

Phoronix - Fri, 06/03/2022 - 17:21
It appears that with upcoming AMD Zen 4 processors there will finally be Virtual NMI (VNMI) support for virtualization, a feature Intel CPUs have supported for well more than the past decade...

Premium Special To Celebrate Phoronix's 18th Birthday

Phoronix - Fri, 06/03/2022 - 15:30
This Sunday, 5 June, marks the 18th birthday for Phoronix.com since I started this website to focus on Linux hardware reviews and performance testing. To mark the occasion, there will be a Phoronix Premium special if you wish to go ad-free on the site and enjoy multi-page articles on a single page while helping to hopefully ensure a successful 19th year for Phoronix...

Fedora Server 37 Looking At Providing A KVM-Optimized Image

Phoronix - Fri, 06/03/2022 - 15:00
Fedora Server working group stakeholders are looking at offering an optimized KVM VM disk image as part of their offerings with the F37 release later this year...

How static linking works on Linux

opensource.com - Fri, 06/03/2022 - 15:00
How static linking works on Linux Jayashree Hutt… Fri, 06/03/2022 - 03:00 Register or Login to like Register or Login to like

Code for applications written using C usually has multiple source files, but ultimately you will need to compile them into a single executable.

You can do this in two ways: by creating a static library or a dynamic library (also called a shared library). These two types of libraries vary in terms of how they are created and linked. Your choice of which to use depends on your use case.

In a previous article, I demonstrated how to create a dynamically linked executable, which is the more commonly used method. In this article, I explain how to create a statically linked executable.

Programming and development Red Hat Developers Blog Programming cheat sheets Try for free: Red Hat Learning Subscription eBook: An introduction to programming with Bash Bash shell scripting cheat sheet eBook: Modernizing Enterprise Java Using a linker with static libraries

A linker is a command that combines several pieces of a program together and reorganizes the memory allocation for them.

The functions of a linker include:

  • Integrating all the pieces of a program
  • Figuring out a new memory organization so that all the pieces fit together
  • Reviving addresses so that the program can run under the new memory organization
  • Resolving symbolic references

As a result of all these linker functionalities, a runnable program called an executable is created.

Static libraries are created by copying all necessary library modules used in a program into the final executable image. The linker links static libraries as a last step in the compilation process. An executable is created by resolving external references, combining the library routines with program code.

Create the object files

Here's an example of a static library, along with the linking process. First, create the header file mymath.h with these function signatures:

int add(int a, int b);
int sub(int a, int b);
int mult(int a, int b);
int divi(int a, int b);

Create add.c, sub.c , mult.c and divi.c with these function definitions:

// add.c
int add(int a, int b){
return (a+b);
}

//sub.c
int sub(int a, int b){
return (a-b);
}

//mult.c
int mult(int a, int b){
return (a*b);
}

//divi.c
int divi(int a, int b){
return (a/b);
}

Now generate object files add.o, sub.o, mult.o, and divi.o using GCC:

$ gcc -c add.c sub.c mult.c divi.c

The -c option skips the linking step and creates only object files.

Create a static library called libmymath.a, then remove the object files, as they're no longer required. (Note that using a trash command is safer than rm.)

$ ar rs libmymath.a add.o sub.o mult.o divi.o
$ trash *.o
$ ls
add.c  divi.c  libmymath.a  mult.c  mymath.h  sub.c

You have now created a simple example math library called libmymath, which you can use in C code. There are, of course, very complex C libraries out there, and this is the process their developers use to generate the final product that you and I install for use in C code.

Next, use your math library in some custom code and then link it.

Create a statically linked application

Suppose you've written a command for mathematics. Create a file called mathDemo.c and paste this code into it:

#include
#include
#include

int main()
{
  int x, y;
  printf("Enter two numbers\n");
  scanf("%d%d",&x,&y);
 
  printf("\n%d + %d = %d", x, y, add(x, y));
  printf("\n%d - %d = %d", x, y, sub(x, y));
  printf("\n%d * %d = %d", x, y, mult(x, y));

  if(y==0){
    printf("\nDenominator is zero so can't perform division\n");
      exit(0);
  }else{
      printf("\n%d / %d = %d\n", x, y, divi(x, y));
      return 0;
  }
}

Notice that the first line is an include statement referencing, by name, your own libmymath library.

Create an object file called mathDemo.o for mathDemo.c:

$ gcc -I . -c mathDemo.c

The -I option tells GCC to search for header files listed after it. In this case, you're specifying the current directory, represented by a single dot (.).

Link mathDemo.o with libmymath.a to create the final executable. There are two ways to express this to GCC.

You can point to the files:

$ gcc -static -o mathDemo mathDemo.o libmymath.a

Alternately, you can specify the library path along with the library name:

$ gcc -static -o mathDemo -L . mathDemo.o -lmymath

In the latter example, the -lmymath option tells the linker to link the object files present in the libmymath.a with the object file mathDemo.o to create the final executable. The -L option directs the linker to look for libraries in the following argument (similar to what you would do with -I).

Analyzing the result

Confirm that it's statically linked using the file command:

$ file mathDemo
mathDemo: ELF 64-bit LSB executable, x86-64...
statically linked, with debug_info, not stripped

Using the ldd command, you can see that the executable is not dynamically linked:

$ ldd ./mathDemo
        not a dynamic executable

You can also check the size of the mathDemo executable:

$ du -h ./mathDemo
932K    ./mathDemo

In the example from my previous article, the dynamic executable took up just 24K.

Run the command to see it work:

$ ./mathDemo
Enter two numbers
10
5

10 + 5 = 15
10 - 5 = 5
10 * 5 = 50
10 / 5 = 2

Looks good!

When to use static linking

Dynamically linked executables are generally preferred over statically linked executables because dynamic linking keeps an application's components modular. Should a library receive a critical security update, it can be easily patched because it exists outside of the applications that use it.

When you use static linking, a library's code gets "hidden" within the executable you create, meaning the only way to patch it is to re-compile and re-release a new executable every time a library gets an update—and you have better things to do with your time, trust me.

However, static linking is a reasonable option if the code of a library exists either in the same code base as the executable using it or in specialized embedded devices that are expected to receive no updates.

Learn how to combine multiple C object files into a single executable with static libraries.

Image by:

Image by Mapbox Uncharted ERG, CC-BY 3.0 US

Programming Linux What to read next How dynamic linking for modular libraries works on Linux This work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License. Register or Login to post a comment.

Linux 5.19 Completes Multi-Platform Support For Intel PXA/XScale, HPE GXP SoC Added

Phoronix - Fri, 06/03/2022 - 12:00
Another set of Arm SoC and platform changes have been submitted for the in-development Linux 5.19 merge window...

Pages