Saturday, November 5, 2022

Simple dlopen-example

        A simple sample for checking whether a .so in Linux both dynamically linked & dynamically loaded-linked in a process will share the same address space. also how the cases of multiple loads are handled.

https://github.com/tvijayas/dlopen-example/blob/main/README.md

main.cpp


int main(void){

   void *handle1,*handle2;
   void (*IncCount)(void);
   void (*GetCount)(void);
   char *error;

   /*lib reference count=1*/
   handle1 = dlopen("libmylib.so", RTLD_LAZY);
   
   if (!handle1) {
      fprintf(stderr, "%s\n", dlerror());
      exit(EXIT_FAILURE);
   }
                 
   dlerror();/* Clear any existing error */
   
   *(void **) (&IncCount) = dlsym(handle1, "increment_count");
   *(void **) (&GetCount) = dlsym(handle1, "get_count");
   
   IncCount();
   
   error = dlerror();
   if (error != NULL) {
      fprintf(stderr, "%s\n", error);
      exit(EXIT_FAILURE);
   }       
                          
   cout<< "I'm in main" <<endl;
   increment_count();
   cout<< get_count() <<endl;
   
   /*lib reference count=2*/
   handle2 = dlopen("libmylib.so", RTLD_LAZY);
   
   if (!handle2) {
      fprintf(stderr, "%s\n", dlerror());
      exit(EXIT_FAILURE);
   }
   
   cout<< "handle1 = " <<(unsigned int)handle1 <<endl;
   cout<< "handle2 = " <<(unsigned int)handle2 <<endl;
   
   /*lib reference count reduced from 2 to 1*/
   dlclose(handle1);
   
   /*handle1 still works as the reference to the library not 0*/
   *(void **) (&IncCount) = dlsym(handle1, "increment_count");
   increment_count();
   cout<< get_count() <<endl;
   
   /*handle2 also works*/
   *(void **) (&IncCount) = dlsym(handle2, "increment_count");
   increment_count();
   cout<< get_count() <<endl;
   
   /*lib reference count reduced from 1 to 0*/
   dlclose(handle1);
      
   return 0;
}


SharedSrc.c

#include "stdio.h"


static int count = 0;
int get_count(void){
  return count;
}

void increment_count(void){
  count++;
}

__attribute__((constructor)) void lib_init(void){
  fprintf(stderr,"%s \n","mylib loaded");   
}

__attribute__((destructor)) void lib_exit(void){
  fprintf(stderr,"%s \n","mylib unload");
}


Output: 
       mylib loaded 
       I'm in main 
       2 
       handle1 = 2439537088 
       handle2 = 2439537088 
       3 
       4 
       mylib unload 


Friday, February 11, 2022

Running flutter engine with Apps on Raspberry Pi3 and Pi4 using yocto build

Getting Started with Flutter on Raspberry Pi

This guide will help you set up the Flutter engine and apps on your Raspberry Pi. All necessary information is available at the meta-flutter GitHub repository. The apps utilize direct DRM for display, bypassing the need for Wayland Weston.

Compatibility

Development Host Requirements

  • Linux Ubuntu 20.04

Building Flutter Engine and Apps with Yocto Scripts are available to automate workspace creation and image building. Find them at the flutter-yocto-setup-build GitHub repository. Execute the script to create a working folder and start the build. Refer to the README for detailed instructions.

Flashing the Image to an SD Card After building, you’ll receive an image for flashing. Use a tool like Etcher for this process. The image will be located in the raspberrypi3-64/tmp/deploy/images/raspberrypi3-64 directory, with a name similar to core-image-minimal-raspberrypi3-64-20220211073219.rootfs.rpi-sdimg.


Remember to always check the latest documentation and community resources for updates and support. Happy coding! 😊







Wednesday, November 17, 2021

Creating a GPT Partitioned image from Scratch in Linux

Some points for later understanding and usage

Protective MBR is for backward comparability.

Primary GPT size = 512 * 34 = 17408 bytes or 17Kb

Secondary GPT size = 512  * 33 = 16896 bytes

 

We will create a 100Mb disk(my.img) with three partitions P1 -> 30MB P2-> 30Mb and P2->Rest

=============Script Start===============

#!/bin/bash

truncate -s 100MiB my.img

parted my.img mklabel gpt /*Create a GPT Table*/
parted my.img mkpart primary ext4 2MiB 30MiB /*create Partition P1*/
parted my.img mkpart primary ext4 30MiB 60MiB /*create Partition P2*/
parted my.img mkpart primary ext4 60MiB 99MiB /*create Partition P3*/

/*Find the free loop device*/
(base) vijay@INDD611T72:~$ sudo losetup -f
[sudo] password for vijay:
/dev/loop2
 

sudo losetup -P /devloop2p1 my.img
sudo mkfs.ext4 /dev/loop2p1
sudo mkdir -p /mnt/P1
sudo mkdir -p /mnt/P2
sudo mkdir -p /mnt/P3
sudo mount /dev/loop2p1 /mnt/P1
sudo mount /dev/loop2p2 /mnt/P2
sudo mount /dev/loop2p3 /mnt/P3

#Copy the required files to the respective partitions

sudo sync
sudo umount /mnt/P1
sudo umount /mnt/P2
sudo umount /mnt/P3
sudo rm -rf /mnt/P1
sudo rm -rf /mnt/P2
sudo rm -rf /mnt/P3
sudo losetup -d /dev/loop2

=============Script End=============== 

Extracting GPT table:

We can extract the GPT table out from the image using following dd commands.

dd if=./my.img of=./gpt-prim.bin skip=0 bs=512 count=34   

 /** primary (34 * 512) bytes**/

 

dd if=./my.img of=./gpt-bkup.bin skip=204767 bs=512 count=33 

/**  100mb -(512*33) = (204767*512) **/

Steps done in PC for reference:

 

 

 

 

 Copy the contents after mounting each partitions individually.

Ref: https://unix.stackexchange.com/questions/87183/how-to-create-a-formatted-partition-image-file-from-scratch

Simple dlopen-example

        A simple sample for checking whether a .so in Linux both dynamically linked & dynamically loaded-linked in a process will share ...