aboutsummaryrefslogtreecommitdiffstats
path: root/lib/lufa/Demos/Device/ClassDriver/AudioInput/AudioInput.h
diff options
context:
space:
mode:
authorIBNobody <ibnobody@gmail.com>2016-04-16 19:32:29 -0500
committerIBNobody <ibnobody@gmail.com>2016-04-16 19:32:29 -0500
commitbc65f73c649b3e8edffcff841a84f9d1e0798bab (patch)
tree839609cd12ea2a4168ad1bb92a398c850f22bddf /lib/lufa/Demos/Device/ClassDriver/AudioInput/AudioInput.h
parent4aea806aa8aa585930a89b67d7206050ac6adb3a (diff)
parentc83aa16f1d614c1c10f7597a67ffb9f2ae871951 (diff)
downloadfirmware-bc65f73c649b3e8edffcff841a84f9d1e0798bab.tar.gz
firmware-bc65f73c649b3e8edffcff841a84f9d1e0798bab.tar.bz2
firmware-bc65f73c649b3e8edffcff841a84f9d1e0798bab.zip
Merge remote-tracking branch 'remotes/jackhumbert/master' into personal_atomic_planck
Diffstat (limited to 'lib/lufa/Demos/Device/ClassDriver/AudioInput/AudioInput.h')
0 files changed, 0 insertions, 0 deletions
> 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
#include <elf.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>

#include <xen/elfnote.h>

#define ELFNOTE_NAME(_n_) ((void*)(_n_) + sizeof(*(_n_)))
#define ELFNOTE_DESC(_n_) (ELFNOTE_NAME(_n_) + (((_n_)->n_namesz+3)&~3))
#define ELFNOTE_NEXT(_n_) (ELFNOTE_DESC(_n_) + (((_n_)->n_descsz+3)&~3))

#ifndef ELFSIZE
#include <limits.h>
#if UINT_MAX == ULONG_MAX
#define ELFSIZE 32
#else
#define ELFSIZE 64
#endif
#endif

#if (ELFSIZE == 32)
typedef Elf32_Nhdr Elf_Nhdr;
typedef Elf32_Half Elf_Half;
typedef Elf32_Word Elf_Word;
#elif (ELFSIZE == 64)
typedef Elf64_Nhdr Elf_Nhdr;
typedef Elf64_Half Elf_Half;
typedef Elf64_Word Elf_Word;
#else
#error "Unknown ELFSIZE"
#endif

static void print_string_note(const char *prefix, Elf_Nhdr *note)
{
	printf("%s: %s\n", prefix, (const char *)ELFNOTE_DESC(note));
}

static void print_numeric_note(const char *prefix,Elf_Nhdr *note)
{
	switch (note->n_descsz)
	{
	case 4:
		printf("%s: %#010" PRIx32 " (4 bytes)\n",
		       prefix, *(uint32_t *)ELFNOTE_DESC(note));
		break;
	case 8:
		printf("%s: %#018" PRIx64 " (8 bytes)\n",
		       prefix, *(uint64_t *)ELFNOTE_DESC(note));
		break;
	default:
		printf("%s: unknown data size %#x\n", prefix, note->n_descsz);
		break;
	}
}

static inline int is_elf(void *image)
{
	/*
	 * Since we are only accessing the e_ident field we can
	 * acccess the bytes directly without needing to figure out
	 * which version of Elf*_Ehdr structure to use.
	 */
	const unsigned char *hdr = image;
	return ( hdr[EI_MAG0] == ELFMAG0 &&
		 hdr[EI_MAG1] == ELFMAG1 &&
		 hdr[EI_MAG2] == ELFMAG2 &&
		 hdr[EI_MAG3] == ELFMAG3 );
}

static inline unsigned char ehdr_class(void *image)
{
	/*
	 * Since we are only accessing the e_ident field we can
	 * acccess the bytes directly without needing to figure out
	 * which version of Elf*_Ehdr structure to use.
	 */
	const unsigned char *hdr = image;
	switch (hdr[EI_CLASS])
	{
	case ELFCLASS32:
	case ELFCLASS64:
		return hdr[EI_CLASS];
	default:
		fprintf(stderr, "Unknown ELF class %d\n", hdr[EI_CLASS]);
		exit(1);
	}
}

static inline Elf_Half ehdr_shnum(void *image)
{
	switch (ehdr_class(image))
	{
	case ELFCLASS32:
		return ((Elf32_Ehdr *)image)->e_shnum;
	case ELFCLASS64:
		return ((Elf64_Ehdr *)image)->e_shnum;
	default:
		exit(1);
	}
}

static inline Elf_Word shdr_type(void *image, int shnum)
{
	switch (ehdr_class(image))
	{
	case ELFCLASS32:
	{
		Elf32_Ehdr *ehdr = (Elf32_Ehdr *)image;
		Elf32_Shdr *shdr = (Elf32_Shdr*)(image + ehdr->e_shoff +
						 (shnum*ehdr->e_shentsize));
		return shdr->sh_type;
	}
	case ELFCLASS64:
	{
		Elf64_Ehdr *ehdr = (Elf64_Ehdr *)image;
		Elf64_Shdr *shdr = (Elf64_Shdr*)(image + ehdr->e_shoff +
						 (shnum*ehdr->e_shentsize));
		return shdr->sh_type;
	}
	default:
		exit(1);
	}
}

static inline const char *shdr_name(void *image, int shnum)
{
	const char *shstrtab;

	switch (ehdr_class(image))
	{
	case ELFCLASS32:
	{
		Elf32_Ehdr *ehdr = (Elf32_Ehdr *)image;
		Elf32_Shdr *shdr;
		/* Find the section-header strings table. */
		if ( ehdr->e_shstrndx == SHN_UNDEF )
			return NULL;
		shdr = (Elf32_Shdr *)(image + ehdr->e_shoff +
				      (ehdr->e_shstrndx*ehdr->e_shentsize));
		shstrtab = image + shdr->sh_offset;

		shdr= (Elf32_Shdr*)(image + ehdr->e_shoff +
				    (shnum*ehdr->e_shentsize));
		return &shstrtab[shdr->sh_name];
	}
	case ELFCLASS64:
	{
		Elf64_Ehdr *ehdr = (Elf64_Ehdr *)image;
		Elf64_Shdr *shdr;
		/* Find the section-header strings table. */
		if ( ehdr->e_shstrndx == SHN_UNDEF )
			return NULL;
		shdr = (Elf64_Shdr *)(image + ehdr->e_shoff +
				      (ehdr->e_shstrndx*ehdr->e_shentsize));
		shstrtab = image + shdr->sh_offset;

		shdr= (Elf64_Shdr*)(image + ehdr->e_shoff +
				    (shnum*ehdr->e_shentsize));
		return &shstrtab[shdr->sh_name];