Trying to install glibc source on Ubuntu 22.04
Olivia Zamora
BeIow is the source code for a C program I use to memory map files. This program is compiled to a shared object and linked into the main program. Its only function is to create a memory mapping for a file.
#define _GNU_SOURCE
#define _LARGE_TIME_API
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <errno.h>
int64_t * create_file_mmap(const char *filename, const char *mode, int64_t *return_buffer) { FILE * fd = fopen(filename, mode); char buf[1024]; if (fd == 0) { strcpy(buf, strerror(errno)); printf("%d --> %s\n", errno, buf); } int fd_I = fileno(fd); int64_t return_buf[2]; off_t offset = 0; int seek_pos = fseeko(fd, offset, SEEK_END); off_t file_size = ftello(fd); double * map_ptr; static int64_t ret_val; //return_array[2]; map_ptr = (double *)mmap(0, file_size, PROT_READ, MAP_SHARED, fd_I, 0); if (errno != 0) { strcpy(buf, strerror(errno)); printf("%d --> %s\n", errno, buf); } ret_val = (int64_t)map_ptr; return_buf[0] = ret_val; return_buf[1] = file_size; int64_t *return_buf_ptr = return_buf; fclose(fd); // THIS IS WHERE THE ERROR IS SIGNALLED return return_buf_ptr; //ret_val;
}I compile it with:
gcc -ggdb -shared -fPIC -D_FILE_OFFSET_BITS=64 -ldl -lc -o Memory_Map_File.o Memory_Map_File.c. I run it on my local Ubuntu 20.04 (with and without GDB) and it works perfectly. However, when I set up a temporary droplet at Digital Ocean with Ubuntu 22.04, at the line fclose(fd) shown above, I get this segfault:
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7e14c70 in __GI__IO_setb (f=f@entry=0x4097c0, b=b@entry=0x0, eb=eb@entry=0x0, a=a@entry=0) at ./libio/genops.c:338
338 ./libio/genops.c: No such file or directory.
(gdb) bt
#0 0x00007ffff7e14c70 in __GI__IO_setb (f=f@entry=0x4097c0, b=b@entry=0x0, eb=eb@entry=0x0, a=a@entry=0) at ./libio/genops.c:338
#1 0x00007ffff7e12f55 in _IO_new_file_close_it (fp=fp@entry=0x4097c0) at ./libio/fileops.c:153
#2 0x00007ffff7e05d8f in _IO_new_fclose (fp=0x4097c0) at ./libio/iofclose.c:53
#3 0x00007ffff7d5d2b3 in create_file_mmap ( filename=0x7ffff7fb8240 <input_fname> "/opt/P01_SH/_Test_Data/Random_Double_4000.bin", mode=0x7ffff7fb826e <file_mode_input> "rb", return_buffer=0x7ffff7fba0f0 <mmap_return_buffer>) at Memory_Map_File.c:48
#4 0x00007ffff7fb7044 in MMap () from /opt/P01_SH/Calc_XYZ/Calc_XYZ.so
#5 0x0000000000401280 in main (argc=1, argv=0x7fffffffe448) at Call_Create_Threads_in_C-Calc_XYZ.c:68The main issue is: ./libio/genops.c: No such file or directory. /libio/genops.c is part of the glibc, so my problem appears to be linking to glibc. Here's how I link:
sudo nasm -f elf64 -g -F dwarf Calc_XYZ.asm
sudo ld -shared Calc_XYZ.o /opt/P01_SH/_Debug_Wrappers_in_C/Create_Threads_in_C-Calc_XYZ.o /opt/P01_SH/_Library/Sprintf_in_C.o /opt/P01_SH/_Library/Write_Data_in_C.o /opt/P01_SH/_Library/Timer_for_NASM.o /opt/P01_SH/_Library/POSIX_Shared_Memory.o /opt/P01_SH/_Library/Virtual_Memory.o /opt/P01_SH/_Library/Available_Memory.o /opt/P01_SH/_Library/Memory_Map_File.o -ldl -lrt -lpthread -lc -o Calc_XYZ.soI added -lc to link to glibc, but I haven't found any confirmation that that's what I should use. Both -libc and -glibc do not work.
The problem apparently is that I need the source for ./libio/genops.c. My research led me to Ciro Santilli's answer at Get source code for libc for debugging in gdb where he shows code to download and install the source. I followed his instructions line by line but at the line sudo sed -Ei 's/^# deb-src /deb-src /' I get: sed: no input files.
I also tried apt source glibc but I got this:
Reading package lists... Done
E: You must put some 'deb-src' URIs in your sources.listLocally the glibc version is 2.31 and in the cloud it's 2.35. I don't know why the source is not a problem locally but it is a problem in the cloud. Locally I did no extra steps to add the glibc source when I originally built Ubuntu. However, this is a droplet at Digital Ocean, and it comes fairly "bare" so I have to install a lot of software (e.g. gcc, nasm, etc).
So my questions are:
Does the segfault message I received above mean that I need the source for genops.c, and therefore need to download the source?
How do I download the source, in view of the message I got when I ran Ciro Santilli's code?
If not that, how can I solve this?
In the future, as I support end users with multiple versions of glibc, will I face version problem with glibc?
Thanks for any help with this.
18 Related questions 1237 How do I use extern to share variables between source files? 237 Multiple glibc libraries on a single host 197 How can I link to a specific glibc version? Related questions 1237 How do I use extern to share variables between source files? 237 Multiple glibc libraries on a single host 197 How can I link to a specific glibc version? 529 How do you get assembler output from C/C++ source in GCC? 79 Check glibc version for a particular gcc compiler 90 Why is statically linking glibc discouraged? 8 Compiling glibc from source with debug symbols 26 Where are syscalls located in glibc source 0 install g++ without update glibc 0 Glibc link difference causing segmentation fault Load 7 more related questions Show fewer related questions Reset to default